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