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