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