Add:core:Added navit_set_vehicle_by_name halper function
[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         if (this_->ready == 3)
212                 navit_draw_async(this_, 0);
213 }
214
215 int
216 navit_get_ready(struct navit *this_)
217 {
218         return this_->ready;
219 }
220
221
222
223 void
224 navit_draw_displaylist(struct navit *this_)
225 {
226         if (this_->ready == 3)
227                 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
228 }
229
230 static void
231 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
232 {
233         int updated;
234         if (attr->type != attr_route_status)
235                 return;
236         updated=attr->u.num;
237         if (this_->ready != 3)
238                 return;
239         if (updated != route_status_path_done_new)
240                 return;
241         if (this_->vehicle) {
242                 if (this_->vehicle->follow_curr == 1)
243                         return;
244                 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
245                         this_->vehicle->follow_curr=this_->vehicle->follow;
246         }
247         navit_draw(this_);
248 }
249
250 void
251 navit_handle_resize(struct navit *this_, int w, int h)
252 {
253         struct map_selection sel;
254         int callback=(this_->ready == 1);
255         this_->ready |= 2;
256         if (this_->w != w || this_->h != h) {
257                 memset(&sel, 0, sizeof(sel));
258                 this_->w=w;
259                 this_->h=h;
260                 sel.u.p_rect.rl.x=w;
261                 sel.u.p_rect.rl.y=h;
262                 transform_set_screen_selection(this_->trans, &sel);
263                 graphics_init(this_->gra);
264                 graphics_set_rect(this_->gra, &sel.u.p_rect);
265         }
266         if (callback) 
267                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
268         if (this_->ready == 3)
269                 navit_draw(this_);
270 }
271
272 static void
273 navit_resize(void *data, int w, int h)
274 {
275         struct navit *this=data;
276         if (!this->ignore_graphics_events)
277                 navit_handle_resize(this, w, h);
278 }
279
280 int
281 navit_get_width(struct navit *this_)
282 {
283         return this_->w;
284 }
285
286
287 int
288 navit_get_height(struct navit *this_)
289 {
290         return this_->h;
291 }
292
293 static void
294 navit_popup(void *data)
295 {
296         struct navit *this_=data;
297         popup(this_, 1, &this_->pressed);
298         this_->button_timeout=NULL;
299         this_->popped=1;
300 }
301
302
303 int
304 navit_ignore_button(struct navit *this_)
305 {
306         if (this_->ignore_button)
307                 return 1;
308         this_->ignore_button=1;
309         return 0;
310 }
311
312 void
313 navit_ignore_graphics_events(struct navit *this_, int ignore)
314 {
315         this_->ignore_graphics_events=ignore;
316 }
317
318 static void
319 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
320 {
321         struct coord co,cn;
322         struct coord c,*cp;
323         int yaw;
324         double angleo,anglen;
325
326         if (!transform_reverse(tr, old, &co))
327                 return;
328         if (rot) {
329                 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
330                 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
331                 yaw=transform_get_yaw(tr)+angleo-anglen;
332                 transform_set_yaw(tr, yaw % 360);
333         }
334         if (!transform_reverse(tr, new, &cn))
335                 return;
336         cp=transform_get_center(tr);
337         c.x=cp->x+co.x-cn.x;
338         c.y=cp->y+co.y-cn.y;
339         dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
340         transform_set_center(tr, &c);
341 }
342
343 static void
344 navit_set_timeout(struct navit *this_)
345 {
346         struct attr follow;
347         follow.type=attr_follow;
348         follow.u.num=this_->center_timeout;
349         navit_set_attr(this_, &follow);
350 }
351
352 int
353 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
354 {
355         int border=16;
356
357         dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
358         callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
359         if (this_->ignore_button) {
360                 this_->ignore_button=0;
361                 return 0;
362         }
363         if (pressed) {
364                 this_->pressed=*p;
365                 this_->last=*p;
366                 this_->zoomed=0;
367                 if (button == 1) {
368                         this_->button_pressed=1;
369                         this_->moved=0;
370                         this_->popped=0;
371                         if (popup_callback)
372                                 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
373                 }
374                 if (button == 2)
375                         navit_set_center_screen(this_, p, 1);
376                 if (button == 3)
377                         popup(this_, button, p);
378                 if (button == 4 && this_->use_mousewheel) {
379                         this_->zoomed = 1;
380                         navit_zoom_in(this_, 2, p);
381                 }
382                 if (button == 5 && this_->use_mousewheel) {
383                         this_->zoomed = 1;
384                         navit_zoom_out(this_, 2, p);
385                 }
386         } else {
387
388                 this_->button_pressed=0;
389                 if (this_->button_timeout) {
390                         event_remove_timeout(this_->button_timeout);
391                         this_->button_timeout=NULL;
392                         if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
393                                 navit_set_center_screen(this_, p, !this_->zoomed);
394                         }
395                 }
396                 if (this_->motion_timeout) {
397                         event_remove_timeout(this_->motion_timeout);
398                         this_->motion_timeout=NULL;
399                 }
400                 if (this_->moved) {
401                         struct point pr;
402                         pr.x=this_->w/2;
403                         pr.y=this_->h;
404 #if 0
405                         update_transformation(this_->trans, &this_->pressed, p, &pr);
406 #else
407                         update_transformation(this_->trans, &this_->pressed, p, NULL);
408 #endif
409                         graphics_draw_drag(this_->gra, NULL);
410                         graphics_overlay_disable(this_->gra, 0);
411                         if (!this_->zoomed) 
412                                 navit_set_timeout(this_);
413                         navit_draw(this_);
414                 } else
415                         return 1;
416         }
417         return 0;
418 }
419
420 static void
421 navit_button(void *data, int pressed, int button, struct point *p)
422 {
423         struct navit *this=data;
424         dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
425         if (!this->ignore_graphics_events) {
426                 if (! this->popup_callback)
427                         this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
428                 navit_handle_button(this, pressed, button, p, this->popup_callback);
429         }
430 }
431
432
433 static void
434 navit_motion_timeout(struct navit *this_)
435 {
436         int dx, dy;
437
438         if (this_->drag_bitmap) {
439                 struct point point;
440                 point.x=(this_->current.x-this_->pressed.x);
441                 point.y=(this_->current.y-this_->pressed.y);
442                 if (graphics_draw_drag(this_->gra, &point)) {
443                         graphics_overlay_disable(this_->gra, 1);
444                         graphics_draw_mode(this_->gra, draw_mode_end);
445                         this_->moved=1;
446                         this_->motion_timeout=NULL;
447                         return;
448                 }
449         } 
450         dx=(this_->current.x-this_->last.x);
451         dy=(this_->current.y-this_->last.y);
452         if (dx || dy) {
453                 struct transformation *tr;
454                 struct point pr;
455                 this_->last=this_->current;
456                 graphics_overlay_disable(this_->gra, 1);
457                 tr=transform_dup(this_->trans);
458                 pr.x=this_->w/2;
459                 pr.y=this_->h;
460 #if 0
461                 update_transformation(tr, &this_->pressed, &this_->current, &pr);
462 #else
463                 update_transformation(tr, &this_->pressed, &this_->current, NULL);
464 #endif
465 #if 0
466                 graphics_displaylist_move(this_->displaylist, dx, dy);
467 #endif
468                 graphics_draw_cancel(this_->gra, this_->displaylist);
469                 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
470                 transform_destroy(tr);
471                 this_->moved=1;
472         }
473         this_->motion_timeout=NULL;
474         return;
475 }
476
477 void
478 navit_handle_motion(struct navit *this_, struct point *p)
479 {
480         int dx, dy;
481
482         if (this_->button_pressed && !this_->popped) {
483                 dx=(p->x-this_->pressed.x);
484                 dy=(p->y-this_->pressed.y);
485                 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
486                         this_->moved=1;
487                         if (this_->button_timeout) {
488                                 event_remove_timeout(this_->button_timeout);
489                                 this_->button_timeout=NULL;
490                         }
491                         this_->current=*p;
492                         if (! this_->motion_timeout_callback)
493                                 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
494                         if (! this_->motion_timeout)
495                                 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
496                 }
497         }
498 }
499
500 static void
501 navit_motion(void *data, struct point *p)
502 {
503         struct navit *this=data;
504         if (!this->ignore_graphics_events) 
505                 navit_handle_motion(this, p);
506 }
507
508 static void
509 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
510 {
511         struct coord c1, c2, *center;
512         if (scale < this_->zoom_min)
513                 scale=this_->zoom_min;
514         if (scale > this_->zoom_max)
515                 scale=this_->zoom_max;
516         if (p)
517                 transform_reverse(this_->trans, p, &c1);
518         transform_set_scale(this_->trans, scale);
519         if (p) {
520                 transform_reverse(this_->trans, p, &c2);
521                 center = transform_center(this_->trans);
522                 center->x += c1.x - c2.x;
523                 center->y += c1.y - c2.y;
524         }
525         if (draw)
526                 navit_draw(this_);
527 }
528
529 /**
530  * @brief Automatically adjusts zoom level
531  *
532  * This function automatically adjusts the current
533  * zoom level according to the current speed.
534  *
535  * @param this_ The navit struct
536  * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
537  * @param speed The vehicles speed in meters per second
538  * @param dir The direction into which the vehicle moves
539  */
540 static void
541 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
542 {
543         struct point pc;
544         int distance,w,h;
545         double new_scale;
546         long scale;
547
548         if (! this_->autozoom_active) {
549                 return;
550         }
551
552         distance = speed * this_->autozoom_secs;
553
554         transform_get_size(this_->trans, &w, &h);
555         transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
556         scale = transform_get_scale(this_->trans);
557
558         /* We make sure that the point we want to see is within a certain range
559          * around the vehicle. The radius of this circle is the size of the
560          * screen. This doesn't necessarily mean the point is visible because of
561          * perspective etc. Quite rough, but should be enough. */
562         
563         if (w > h) {
564                 new_scale = (double)distance / h * 16; 
565         } else {
566                 new_scale = (double)distance / w * 16; 
567         }
568
569         if (abs(new_scale - scale) < 2) { 
570                 return; // Smoothing
571         }
572         
573         if (new_scale >= this_->autozoom_min) {
574                 navit_scale(this_, (long)new_scale, &pc, 0);
575         } else {
576                 if (scale != this_->autozoom_min) {
577                         navit_scale(this_, this_->autozoom_min, &pc, 0);
578                 }
579         }
580 }
581
582 /**
583  * Change the current zoom level, zooming closer to the ground
584  *
585  * @param navit The navit instance
586  * @param factor The zoom factor, usually 2
587  * @param p The invariant point (if set to NULL, default to center)
588  * @returns nothing
589  */
590 void
591 navit_zoom_in(struct navit *this_, int factor, struct point *p)
592 {
593         long scale=transform_get_scale(this_->trans)/factor;
594         if (scale < 1)
595                 scale=1;
596         navit_scale(this_, scale, p, 1);
597 }
598
599 /**
600  * Change the current zoom level
601  *
602  * @param navit The navit instance
603  * @param factor The zoom factor, usually 2
604  * @param p The invariant point (if set to NULL, default to center)
605  * @returns nothing
606  */
607 void
608 navit_zoom_out(struct navit *this_, int factor, struct point *p)
609 {
610         long scale=transform_get_scale(this_->trans)*factor;
611         navit_scale(this_, scale, p, 1);
612 }
613
614 static int
615 navit_cmd_zoom_in(struct navit *this_)
616 {
617         struct point p;
618         if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
619                 navit_zoom_in(this_, 2, &p);
620                 this_->vehicle->follow_curr=this_->vehicle->follow;
621         } else
622                 navit_zoom_in(this_, 2, NULL);
623         return 0;
624 }
625
626 static int
627 navit_cmd_zoom_out(struct navit *this_)
628 {
629         struct point p;
630         if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
631                 navit_zoom_out(this_, 2, &p);
632                 this_->vehicle->follow_curr=this_->vehicle->follow;
633         } else
634                 navit_zoom_out(this_, 2, NULL);
635         return 0;
636 }
637
638
639 static void
640 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
641 {
642         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) 
643                 navit_say(this, in[0]->u.str);
644 }
645
646 static void
647 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
648 {
649         struct pcoord pc;
650         char *description=NULL;
651         if (!in)
652                 return;
653         if (!in[0])
654                 return;
655         pc.pro = transform_get_projection(this->trans);
656         if (ATTR_IS_COORD(in[0]->type)) {
657                 pc.x=in[0]->u.coord->x;
658                 pc.y=in[0]->u.coord->y;
659                 in++;
660         } else if (ATTR_IS_PCOORD(in[0]->type)) {
661                 pc=*in[0]->u.pcoord;
662                 in++;
663         } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
664                 pc.pro=in[0]->u.num;
665                 pc.x=in[1]->u.num;
666                 pc.y=in[2]->u.num;
667                 in+=3;
668         } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
669                 pc.x=in[0]->u.num;
670                 pc.y=in[1]->u.num;
671                 in+=2;
672         } else
673                 return;
674         if (in[0] && ATTR_IS_STRING(in[0]->type))
675                 description=in[0]->u.str;
676         navit_set_destination(this, &pc, description, 1);
677 }
678
679 static struct command_table commands[] = {
680         {"zoom_in",command_cast(navit_cmd_zoom_in)},
681         {"zoom_out",command_cast(navit_cmd_zoom_out)},
682         {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
683         {"say",command_cast(navit_cmd_say)},
684         {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
685         {"set_destination",command_cast(navit_cmd_set_destination)},
686         {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
687 };
688         
689
690 struct navit *
691 navit_new(struct attr *parent, struct attr **attrs)
692 {
693         struct navit *this_=g_new0(struct navit, 1);
694         struct pcoord center;
695         struct coord co;
696         struct coord_geo g;
697         enum projection pro=projection_mg;
698         int zoom = 256;
699         g.lat=53.13;
700         g.lng=11.70;
701
702         this_->self.type=attr_navit;
703         this_->self.u.navit=this_;
704         this_->attr_cbl=callback_list_new();
705
706         this_->orientation=-1;
707         this_->tracking_flag=1;
708         this_->recentdest_count=10;
709         this_->osd_configuration=-1;
710
711         this_->center_timeout = 10;
712         this_->use_mousewheel = 1;
713         this_->autozoom_secs = 10;
714         this_->autozoom_min = 7;
715         this_->autozoom_active = 0;
716         this_->zoom_min = 1;
717         this_->zoom_max = 2097152;
718         this_->follow_cursor = 1;
719
720         this_->trans = transform_new();
721         transform_from_geo(pro, &g, &co);
722         center.x=co.x;
723         center.y=co.y;
724         center.pro = pro;
725         
726         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
727
728         this_->bookmarks=bookmarks_new(&this_->self, this_->trans);
729
730         this_->prevTs=0;
731
732         for (;*attrs; attrs++) {
733                 navit_set_attr_do(this_, *attrs, 1);
734         }
735         this_->displaylist=graphics_displaylist_new();
736         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
737
738         this_->messages = messagelist_new(attrs);
739         
740         return this_;
741 }
742
743 static int
744 navit_set_gui(struct navit *this_, struct gui *gui)
745 {
746         if (this_->gui)
747                 return 0;
748         this_->gui=gui;
749         if (gui_has_main_loop(this_->gui)) {
750                 if (! main_loop_gui) {
751                         main_loop_gui=this_->gui;
752                 } else {
753                         dbg(0,"gui with main loop already active, ignoring this instance");
754                         return 0;
755                 }
756         }
757         return 1;
758 }
759
760 void 
761 navit_add_message(struct navit *this_, char *message)
762 {
763         message_new(this_->messages, message);
764 }
765
766 struct message
767 *navit_get_messages(struct navit *this_)
768 {
769         return message_get(this_->messages);
770 }
771
772 static int
773 navit_set_graphics(struct navit *this_, struct graphics *gra)
774 {
775         if (this_->gra)
776                 return 0;
777         this_->gra=gra;
778         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
779         graphics_add_callback(gra, this_->resize_callback);
780         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
781         graphics_add_callback(gra, this_->button_callback);
782         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
783         graphics_add_callback(gra, this_->motion_callback);
784         return 1;
785 }
786
787 struct graphics *
788 navit_get_graphics(struct navit *this_)
789 {
790         return this_->gra;
791 }
792
793 struct vehicleprofile *
794 navit_get_vehicleprofile(struct navit *this_)
795 {
796         return this_->vehicleprofile;
797 }
798
799 GList *
800 navit_get_vehicleprofiles(struct navit *this_)
801 {
802         return this_->vehicleprofiles;
803 }
804
805 static void
806 navit_projection_set(struct navit *this_, enum projection pro, int draw)
807 {
808         struct coord_geo g;
809         struct coord *c;
810
811         c=transform_center(this_->trans);
812         transform_to_geo(transform_get_projection(this_->trans), c, &g);
813         transform_set_projection(this_->trans, pro);
814         transform_from_geo(pro, &g, c);
815         if (draw)
816                 navit_draw(this_);
817 }
818
819 /**
820  * Start the route computing to a given set of coordinates
821  *
822  * @param navit The navit instance
823  * @param c The coordinate to start routing to
824  * @param description A label which allows the user to later identify this destination in the former destinations selection
825  * @returns nothing
826  */
827 void
828 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
829 {
830         if (c) {
831                 this_->destination=*c;
832                 this_->destination_valid=1;
833         } else
834                 this_->destination_valid=0;
835         char *destination_file = bookmarks_get_destination_file(TRUE);
836         bookmarks_append_coord(this_->bookmarks, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
837         g_free(destination_file);
838         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
839         if (this_->route) {
840                 route_set_destination(this_->route, c, async);
841
842                 if (this_->ready == 3)
843                         navit_draw(this_);
844         }
845 }
846
847 /**
848  * @brief Checks if a route is calculated
849  *
850  * This function checks if a route is calculated.
851  *
852  * @param this_ The navit struct whose route should be checked.
853  * @return True if the route is set, false otherwise.
854  */
855 int
856 navit_check_route(struct navit *this_)
857 {
858         if (this_->route) {
859                 return route_get_path_set(this_->route);
860         }
861
862         return 0;
863 }
864
865 static int
866 navit_former_destinations_active(struct navit *this_)
867 {
868         char *destination_file = bookmarks_get_destination_file(FALSE);
869         FILE *f;
870         int active=0;
871         char buffer[3];
872         f=fopen(destination_file,"r");
873         if (f) {
874                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
875                         active=1;
876                 fclose(f);
877         }
878         g_free(destination_file);
879         return active;
880 }
881
882 static void
883 navit_add_former_destinations_from_file(struct navit *this_)
884 {
885         char *destination_file = bookmarks_get_destination_file(FALSE);
886         struct attr parent={attr_navit, .u.navit=this_};
887         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
888         struct attr *attrs[]={&type, &data, NULL};
889         struct map_rect *mr;
890         struct item *item;
891         int valid=0;
892         struct coord c;
893         struct pcoord pc;
894
895         this_->former_destination=map_new(&parent, attrs);
896         g_free(destination_file);
897         if (!this_->route || !navit_former_destinations_active(this_))
898                 return; 
899         mr=map_rect_new(this_->former_destination, NULL);
900         while ((item=map_rect_get_item(mr))) {
901                 if (item->type == type_former_destination && item_coord_get(item, &c, 1)) 
902                         valid=1;
903         }
904         map_rect_destroy(mr);
905         pc.pro=map_projection(this_->former_destination);
906         pc.x=c.x;
907         pc.y=c.y;
908         if (valid) {
909                 route_set_destination(this_->route, &pc, 1);
910                 this_->destination=pc;
911                 this_->destination_valid=1;
912         }
913 }
914
915
916 void
917 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
918 {
919         va_list ap;
920         char *str1,*str2;
921         va_start(ap, fmt);
922         if (this_->textfile_debug_log && this_->vehicle) {
923                 str1=g_strdup_vprintf(fmt, ap);
924                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
925                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
926                 g_free(str2);
927                 g_free(str1);
928         }
929         va_end(ap);
930 }
931
932 int 
933 navit_speech_estimate(struct navit *this_, char *str)
934 {
935         return speech_estimate_duration(this_->speech, str);
936 }
937
938 void
939 navit_say(struct navit *this_, char *text)
940 {
941         speech_say(this_->speech, text);
942 }
943
944 /**
945  * @brief Toggles the navigation announcer for navit
946  * @param this_ The navit object
947  */
948 static void
949 navit_cmd_announcer_toggle(struct navit *this_)
950 {
951     struct attr attr, speechattr;
952
953     // search for the speech attribute
954     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
955         return;
956     // find out if the corresponding attribute attr_active has been set
957     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
958         // flip it then...
959         attr.u.num = !attr.u.num;
960     } else {
961         // otherwise disable it because voice is enabled by default
962         attr.type = attr_active;
963         attr.u.num = 0;
964     }
965
966     // apply the new state
967     if(!speech_set_attr(speechattr.u.speech, &attr))
968         return;
969
970     // announce that the speech attribute has changed
971     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
972 }
973
974 void
975 navit_speak(struct navit *this_)
976 {
977         struct navigation *nav=this_->navigation;
978         struct map *map=NULL;
979         struct map_rect *mr=NULL;
980         struct item *item;
981         struct attr attr;
982
983     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
984         attr.u.num = 1;
985     dbg(1, "this_.speech->active %i\n", attr.u.num);
986     if(!attr.u.num)
987         return;
988
989         if (nav)
990                 map=navigation_get_map(nav);
991         if (map)
992                 mr=map_rect_new(map, NULL);
993         if (mr) {
994                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
995                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
996                         speech_say(this_->speech, attr.u.str);
997                         navit_add_message(this_, attr.u.str);
998                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
999                 }
1000                 map_rect_destroy(mr);
1001         }
1002 }
1003
1004 static void
1005 navit_window_roadbook_update(struct navit *this_)
1006 {
1007         struct navigation *nav=this_->navigation;
1008         struct map *map=NULL;
1009         struct map_rect *mr=NULL;
1010         struct item *item;
1011         struct attr attr;
1012         struct param_list param[5];
1013         int secs;
1014
1015         dbg(1,"enter\n");
1016         datawindow_mode(this_->roadbook_window, 1);
1017         if (nav)
1018                 map=navigation_get_map(nav);
1019         if (map)
1020                 mr=map_rect_new(map, NULL);
1021         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1022         if (mr) {
1023                 dbg(0,"while loop\n");
1024                 while ((item=map_rect_get_item(mr))) {
1025                         dbg(0,"item=%p\n", item);
1026                         attr.u.str=NULL;
1027                         if (item->type != type_nav_position) {
1028                                 item_attr_get(item, attr_navigation_long, &attr);
1029                                 if (attr.u.str == NULL) {
1030                                         continue;
1031                                 }
1032                                 dbg(2, "Command='%s'\n", attr.u.str);
1033                                 param[0].value=g_strdup(attr.u.str);
1034                         } else
1035                                 param[0].value=_("Position");
1036                         param[0].name=_("Command");
1037
1038                         item_attr_get(item, attr_length, &attr);
1039                         dbg(2, "Length=%d\n", attr.u.num);
1040                         param[1].name=_("Length");
1041
1042                         if ( attr.u.num >= 2000 )
1043                         {
1044                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1045                         }
1046                         else
1047                         {
1048                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1049                         }
1050
1051                         item_attr_get(item, attr_time, &attr);
1052                         dbg(2, "Time=%d\n", attr.u.num);
1053                         secs=attr.u.num/10;
1054                         param[2].name=_("Time");
1055                         if ( secs >= 3600 )
1056                         {
1057                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1058                         }
1059                         else
1060                         {
1061                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1062                         }
1063
1064                         item_attr_get(item, attr_destination_length, &attr);
1065                         dbg(2, "Destlength=%d\n", attr.u.num);
1066                         param[3].name=_("Destination Length");
1067                         if ( attr.u.num >= 2000 )
1068                         {
1069                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1070                         }
1071                         else
1072                         {
1073                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1074                         }
1075
1076                         item_attr_get(item, attr_destination_time, &attr);
1077                         dbg(2, "Desttime=%d\n", attr.u.num);
1078                         secs=attr.u.num/10;
1079                         param[4].name=_("Destination Time");
1080                         if ( secs >= 3600 )
1081                         {
1082                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1083                         }
1084                         else
1085                         {
1086                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1087                         }
1088                         datawindow_add(this_->roadbook_window, param, 5);
1089                 }
1090                 map_rect_destroy(mr);
1091         }
1092         datawindow_mode(this_->roadbook_window, 0);
1093 }
1094
1095 void
1096 navit_window_roadbook_destroy(struct navit *this_)
1097 {
1098         dbg(0, "enter\n");
1099         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1100         this_->roadbook_window=NULL;
1101         this_->roadbook_callback=NULL;
1102 }
1103 void
1104 navit_window_roadbook_new(struct navit *this_)
1105 {
1106         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1107                 return;
1108         }
1109
1110         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1111         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1112         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1113         navit_window_roadbook_update(this_);
1114 }
1115
1116 void
1117 navit_init(struct navit *this_)
1118 {
1119         struct mapset *ms;
1120         struct map *map;
1121         int callback;
1122
1123         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1124         if (!this_->gui) {
1125                 dbg(0,"no gui\n");
1126                 navit_destroy(this_);
1127                 return;
1128         }
1129         if (!this_->gra) {
1130                 dbg(0,"no graphics\n");
1131                 navit_destroy(this_);
1132                 return;
1133         }
1134         dbg(2,"Connecting gui to graphics\n");
1135         if (gui_set_graphics(this_->gui, this_->gra)) {
1136                 struct attr attr_type_gui, attr_type_graphics;
1137                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1138                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1139                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1140                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1141                 dbg(0," for explanations and solutions\n");
1142
1143                 navit_destroy(this_);
1144                 return;
1145         }
1146         dbg(2,"Initializing graphics\n");
1147         dbg(2,"Setting Vehicle\n");
1148         navit_set_vehicle(this_, this_->vehicle);
1149         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1150         if (this_->mapsets) {
1151                 ms=this_->mapsets->data;
1152                 if (this_->route) {
1153                         if ((map=route_get_map(this_->route)))
1154                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1155                         if ((map=route_get_graph_map(this_->route))) {
1156                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1157                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1158                         }
1159                         route_set_mapset(this_->route, ms);
1160                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1161                 }
1162                 if (this_->tracking) {
1163                         tracking_set_mapset(this_->tracking, ms);
1164                         if (this_->route)
1165                                 tracking_set_route(this_->tracking, this_->route);
1166                 }
1167                 if (this_->navigation) {
1168                         if ((map=navigation_get_map(this_->navigation))) {
1169                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1170                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1171                         }
1172                 }
1173                 if (this_->tracking) {
1174                         if ((map=tracking_get_map(this_->tracking))) {
1175                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1176                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1177                         }
1178                 }
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_vehicle_by_name(struct navit *n,const char *name) 
2218 {
2219     struct vehicle *v;
2220     struct attr_iter *iter;
2221     struct attr vehicle_attr, name_attr;
2222
2223         iter=navit_attr_iter_new();
2224
2225     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2226                 v=vehicle_attr.u.vehicle;
2227                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2228                 if (name_attr.type==attr_name) {
2229                         if (!strcmp(name,name_attr.u.str)) {
2230                                 navit_set_attr(n,&vehicle_attr);                                
2231                                 navit_attr_iter_destroy(iter);
2232                                 return 1;
2233                         }
2234                 }
2235         }
2236     navit_attr_iter_destroy(iter);
2237     return 0;
2238 }
2239
2240 int 
2241 navit_set_layout_by_name(struct navit *n,const char *name) 
2242 {
2243     struct layout *l;
2244     struct attr_iter iter;
2245     struct attr layout_attr;
2246
2247     iter.u.list=0x00;
2248
2249     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2250         return 0; //No layouts - nothing to do
2251     }
2252     if (iter.u.list==NULL) {
2253         return 0;
2254     }
2255     
2256     iter.u.list=g_list_first(iter.u.list);
2257     
2258     while(iter.u.list) {
2259         l=(struct layout*)iter.u.list->data;
2260         if (!strcmp(name,l->name)) {
2261             layout_attr.u.layout=l;
2262             layout_attr.type=attr_layout;
2263             navit_set_attr(n,&layout_attr);
2264             iter.u.list=g_list_first(iter.u.list);
2265             return 1;
2266         }
2267         iter.u.list=g_list_next(iter.u.list);
2268     }
2269
2270     iter.u.list=g_list_first(iter.u.list);
2271     return 0;
2272 }
2273
2274 int
2275 navit_block(struct navit *this_, int block)
2276 {
2277         if (block) {
2278                 this_->blocked |= 1;
2279                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2280                         this_->blocked |= 2;
2281                 return 0;
2282         }
2283         if (this_->blocked & 2) {
2284                 this_->blocked=0;
2285                 navit_draw(this_);
2286                 return 1;
2287         }
2288         this_->blocked=0;
2289         return 0;
2290 }
2291
2292 void
2293 navit_destroy(struct navit *this_)
2294 {
2295         /* TODO: destroy objects contained in this_ */
2296         if (this_->vehicle)
2297                 vehicle_destroy(this_->vehicle->vehicle);
2298         if (this_->bookmarks) {
2299                 char *center_file = bookmarks_get_center_file(TRUE);
2300                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2301                 g_free(center_file);
2302                 bookmarks_destroy(this_->bookmarks);
2303         }
2304         callback_destroy(this_->nav_speech_cb);
2305         callback_destroy(this_->roadbook_callback);
2306         callback_destroy(this_->popup_callback);
2307         callback_destroy(this_->motion_timeout_callback);
2308         if(this_->gra)
2309           graphics_remove_callback(this_->gra, this_->resize_callback);
2310         callback_destroy(this_->resize_callback);
2311         if(this_->gra)
2312           graphics_remove_callback(this_->gra, this_->button_callback);
2313         callback_destroy(this_->button_callback);
2314         if(this_->gra)
2315           graphics_remove_callback(this_->gra, this_->motion_callback);
2316         callback_destroy(this_->motion_callback);
2317         g_free(this_);
2318 }
2319
2320 /** @} */