Fix:Core:Added null check to navit_say() to avoid crash when there is no speech entry...
[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
750 struct navit *
751 navit_new(struct attr *parent, struct attr **attrs)
752 {
753         struct navit *this_=g_new0(struct navit, 1);
754         struct pcoord center;
755         struct coord co;
756         struct coord_geo g;
757         enum projection pro=projection_mg;
758         int zoom = 256;
759         g.lat=53.13;
760         g.lng=11.70;
761
762         this_->self.type=attr_navit;
763         this_->self.u.navit=this_;
764         this_->attr_cbl=callback_list_new();
765
766         this_->orientation=-1;
767         this_->tracking_flag=1;
768         this_->recentdest_count=10;
769         this_->osd_configuration=-1;
770
771         this_->center_timeout = 10;
772         this_->use_mousewheel = 1;
773         this_->autozoom_secs = 10;
774         this_->autozoom_min = 7;
775         this_->autozoom_active = 0;
776         this_->zoom_min = 1;
777         this_->zoom_max = 2097152;
778         this_->follow_cursor = 1;
779         this_->radius = 30;
780         this_->border = 16;
781
782         this_->trans = transform_new();
783         this_->trans_cursor = transform_new();
784         transform_from_geo(pro, &g, &co);
785         center.x=co.x;
786         center.y=co.y;
787         center.pro = pro;
788         
789         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
790
791         this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
792
793         this_->prevTs=0;
794
795         for (;*attrs; attrs++) {
796                 navit_set_attr_do(this_, *attrs, 1);
797         }
798         this_->displaylist=graphics_displaylist_new();
799         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
800
801         this_->messages = messagelist_new(attrs);
802         
803         return this_;
804 }
805
806 static int
807 navit_set_gui(struct navit *this_, struct gui *gui)
808 {
809         if (this_->gui)
810                 return 0;
811         this_->gui=gui;
812         if (gui_has_main_loop(this_->gui)) {
813                 if (! main_loop_gui) {
814                         main_loop_gui=this_->gui;
815                 } else {
816                         dbg(0,"gui with main loop already active, ignoring this instance");
817                         return 0;
818                 }
819         }
820         return 1;
821 }
822
823 void 
824 navit_add_message(struct navit *this_, char *message)
825 {
826         message_new(this_->messages, message);
827 }
828
829 struct message
830 *navit_get_messages(struct navit *this_)
831 {
832         return message_get(this_->messages);
833 }
834
835 static int
836 navit_set_graphics(struct navit *this_, struct graphics *gra)
837 {
838         if (this_->gra)
839                 return 0;
840         this_->gra=gra;
841         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
842         graphics_add_callback(gra, this_->resize_callback);
843         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
844         graphics_add_callback(gra, this_->button_callback);
845         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
846         graphics_add_callback(gra, this_->motion_callback);
847         this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
848         graphics_add_callback(gra, this_->predraw_callback);
849         return 1;
850 }
851
852 struct graphics *
853 navit_get_graphics(struct navit *this_)
854 {
855         return this_->gra;
856 }
857
858 struct vehicleprofile *
859 navit_get_vehicleprofile(struct navit *this_)
860 {
861         return this_->vehicleprofile;
862 }
863
864 GList *
865 navit_get_vehicleprofiles(struct navit *this_)
866 {
867         return this_->vehicleprofiles;
868 }
869
870 static void
871 navit_projection_set(struct navit *this_, enum projection pro, int draw)
872 {
873         struct coord_geo g;
874         struct coord *c;
875
876         c=transform_center(this_->trans);
877         transform_to_geo(transform_get_projection(this_->trans), c, &g);
878         transform_set_projection(this_->trans, pro);
879         transform_from_geo(pro, &g, c);
880         if (draw)
881                 navit_draw(this_);
882 }
883
884 /**
885  * Start the route computing to a given set of coordinates
886  *
887  * @param navit The navit instance
888  * @param c The coordinate to start routing to
889  * @param description A label which allows the user to later identify this destination in the former destinations selection
890  * @returns nothing
891  */
892 void
893 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
894 {
895         if (c) {
896                 this_->destination=*c;
897                 this_->destination_valid=1;
898         } else
899                 this_->destination_valid=0;
900         char *destination_file = bookmarks_get_destination_file(TRUE);
901         bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
902         g_free(destination_file);
903         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
904         if (this_->route) {
905                 route_set_destination(this_->route, c, async);
906
907                 if (this_->ready == 3)
908                         navit_draw(this_);
909         }
910 }
911
912 /**
913  * Start the route computing to a given set of coordinates including waypoints
914  *
915  * @param navit The navit instance
916  * @param c The coordinate to start routing to
917  * @param description A label which allows the user to later identify this destination in the former destinations selection
918  * @returns nothing
919  */
920 void
921 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
922 {
923         if (c && count) {
924                 this_->destination=c[count-1];
925                 this_->destination_valid=1;
926         } else
927                 this_->destination_valid=0;
928         char *destination_file = bookmarks_get_destination_file(TRUE);
929         bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
930         g_free(destination_file);
931         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
932         if (this_->route) {
933                 route_set_destinations(this_->route, c, count, async);
934
935                 if (this_->ready == 3)
936                         navit_draw(this_);
937         }
938 }
939
940 /**
941  * @brief Checks if a route is calculated
942  *
943  * This function checks if a route is calculated.
944  *
945  * @param this_ The navit struct whose route should be checked.
946  * @return True if the route is set, false otherwise.
947  */
948 int
949 navit_check_route(struct navit *this_)
950 {
951         if (this_->route) {
952                 return route_get_path_set(this_->route);
953         }
954
955         return 0;
956 }
957
958 static int
959 navit_former_destinations_active(struct navit *this_)
960 {
961         char *destination_file = bookmarks_get_destination_file(FALSE);
962         FILE *f;
963         int active=0;
964         char buffer[3];
965         f=fopen(destination_file,"r");
966         if (f) {
967                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
968                         active=1;
969                 fclose(f);
970         }
971         g_free(destination_file);
972         return active;
973 }
974
975 static void
976 navit_add_former_destinations_from_file(struct navit *this_)
977 {
978         char *destination_file = bookmarks_get_destination_file(FALSE);
979         struct attr parent={attr_navit, .u.navit=this_};
980         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}}, flags={attr_flags, {(void *)1}};
981         struct attr *attrs[]={&type, &data, &flags, NULL};
982         struct map_rect *mr;
983         struct item *item;
984         int i,valid=0,count=0;
985         struct coord c[16];
986         struct pcoord pc[16];
987
988         this_->former_destination=map_new(&parent, attrs);
989         g_free(destination_file);
990         if (!this_->route || !navit_former_destinations_active(this_))
991                 return; 
992         mr=map_rect_new(this_->former_destination, NULL);
993         while ((item=map_rect_get_item(mr))) {
994                 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16))) 
995                         valid=1;
996         }
997         map_rect_destroy(mr);
998         if (valid && count > 0) {
999                 for (i = 0 ; i < count ; i++) {
1000                         pc[i].pro=map_projection(this_->former_destination);
1001                         pc[i].x=c[i].x;
1002                         pc[i].y=c[i].y;
1003                 }
1004                 if (count == 1)
1005                         route_set_destination(this_->route, &pc[0], 1);
1006                 else
1007                         route_set_destinations(this_->route, pc, count, 1);
1008                 this_->destination=pc[count-1];
1009                 this_->destination_valid=1;
1010         }
1011 }
1012
1013
1014 void
1015 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1016 {
1017         va_list ap;
1018         char *str1,*str2;
1019         va_start(ap, fmt);
1020         if (this_->textfile_debug_log && this_->vehicle) {
1021                 str1=g_strdup_vprintf(fmt, ap);
1022                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1023                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1024                 g_free(str2);
1025                 g_free(str1);
1026         }
1027         va_end(ap);
1028 }
1029
1030 int 
1031 navit_speech_estimate(struct navit *this_, char *str)
1032 {
1033         return speech_estimate_duration(this_->speech, str);
1034 }
1035
1036 void
1037 navit_say(struct navit *this_, char *text)
1038 {
1039         if(this_->speech) {
1040                 speech_say(this_->speech, text);
1041         }
1042 }
1043
1044 /**
1045  * @brief Toggles the navigation announcer for navit
1046  * @param this_ The navit object
1047  */
1048 static void
1049 navit_cmd_announcer_toggle(struct navit *this_)
1050 {
1051     struct attr attr, speechattr;
1052
1053     // search for the speech attribute
1054     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1055         return;
1056     // find out if the corresponding attribute attr_active has been set
1057     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1058         // flip it then...
1059         attr.u.num = !attr.u.num;
1060     } else {
1061         // otherwise disable it because voice is enabled by default
1062         attr.type = attr_active;
1063         attr.u.num = 0;
1064     }
1065
1066     // apply the new state
1067     if(!speech_set_attr(speechattr.u.speech, &attr))
1068         return;
1069
1070     // announce that the speech attribute has changed
1071     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1072 }
1073
1074 void
1075 navit_speak(struct navit *this_)
1076 {
1077         struct navigation *nav=this_->navigation;
1078         struct map *map=NULL;
1079         struct map_rect *mr=NULL;
1080         struct item *item;
1081         struct attr attr;
1082
1083     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1084         attr.u.num = 1;
1085     dbg(1, "this_.speech->active %i\n", attr.u.num);
1086     if(!attr.u.num)
1087         return;
1088
1089         if (nav)
1090                 map=navigation_get_map(nav);
1091         if (map)
1092                 mr=map_rect_new(map, NULL);
1093         if (mr) {
1094                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1095                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1096                         speech_say(this_->speech, attr.u.str);
1097                         navit_add_message(this_, attr.u.str);
1098                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1099                 }
1100                 map_rect_destroy(mr);
1101         }
1102 }
1103
1104 static void
1105 navit_window_roadbook_update(struct navit *this_)
1106 {
1107         struct navigation *nav=this_->navigation;
1108         struct map *map=NULL;
1109         struct map_rect *mr=NULL;
1110         struct item *item;
1111         struct attr attr;
1112         struct param_list param[5];
1113         int secs;
1114
1115         dbg(1,"enter\n");
1116         datawindow_mode(this_->roadbook_window, 1);
1117         if (nav)
1118                 map=navigation_get_map(nav);
1119         if (map)
1120                 mr=map_rect_new(map, NULL);
1121         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1122         if (mr) {
1123                 dbg(0,"while loop\n");
1124                 while ((item=map_rect_get_item(mr))) {
1125                         dbg(0,"item=%p\n", item);
1126                         attr.u.str=NULL;
1127                         if (item->type != type_nav_position) {
1128                                 item_attr_get(item, attr_navigation_long, &attr);
1129                                 if (attr.u.str == NULL) {
1130                                         continue;
1131                                 }
1132                                 dbg(2, "Command='%s'\n", attr.u.str);
1133                                 param[0].value=g_strdup(attr.u.str);
1134                         } else
1135                                 param[0].value=_("Position");
1136                         param[0].name=_("Command");
1137
1138                         item_attr_get(item, attr_length, &attr);
1139                         dbg(2, "Length=%d\n", attr.u.num);
1140                         param[1].name=_("Length");
1141
1142                         if ( attr.u.num >= 2000 )
1143                         {
1144                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1145                         }
1146                         else
1147                         {
1148                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1149                         }
1150
1151                         item_attr_get(item, attr_time, &attr);
1152                         dbg(2, "Time=%d\n", attr.u.num);
1153                         secs=attr.u.num/10;
1154                         param[2].name=_("Time");
1155                         if ( secs >= 3600 )
1156                         {
1157                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1158                         }
1159                         else
1160                         {
1161                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1162                         }
1163
1164                         item_attr_get(item, attr_destination_length, &attr);
1165                         dbg(2, "Destlength=%d\n", attr.u.num);
1166                         param[3].name=_("Destination Length");
1167                         if ( attr.u.num >= 2000 )
1168                         {
1169                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1170                         }
1171                         else
1172                         {
1173                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1174                         }
1175
1176                         item_attr_get(item, attr_destination_time, &attr);
1177                         dbg(2, "Desttime=%d\n", attr.u.num);
1178                         secs=attr.u.num/10;
1179                         param[4].name=_("Destination Time");
1180                         if ( secs >= 3600 )
1181                         {
1182                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1183                         }
1184                         else
1185                         {
1186                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1187                         }
1188                         datawindow_add(this_->roadbook_window, param, 5);
1189                 }
1190                 map_rect_destroy(mr);
1191         }
1192         datawindow_mode(this_->roadbook_window, 0);
1193 }
1194
1195 void
1196 navit_window_roadbook_destroy(struct navit *this_)
1197 {
1198         dbg(0, "enter\n");
1199         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1200         this_->roadbook_window=NULL;
1201         this_->roadbook_callback=NULL;
1202 }
1203 void
1204 navit_window_roadbook_new(struct navit *this_)
1205 {
1206         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1207                 return;
1208         }
1209
1210         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1211         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1212         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1213         navit_window_roadbook_update(this_);
1214 }
1215
1216 void
1217 navit_init(struct navit *this_)
1218 {
1219         struct mapset *ms;
1220         struct map *map;
1221         int callback;
1222
1223         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1224
1225         if (!this_->gui && !(this_->flags & 2)) {
1226                 dbg(0,"no gui\n");
1227                 navit_destroy(this_);
1228                 return;
1229         }
1230         if (!this_->gra && !(this_->flags & 1)) {
1231                 dbg(0,"no graphics\n");
1232                 navit_destroy(this_);
1233                 return;
1234         }
1235         dbg(2,"Connecting gui to graphics\n");
1236         if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1237                 struct attr attr_type_gui, attr_type_graphics;
1238                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1239                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1240                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1241                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1242                 dbg(0," for explanations and solutions\n");
1243
1244                 navit_destroy(this_);
1245                 return;
1246         }
1247         dbg(2,"Initializing graphics\n");
1248         dbg(2,"Setting Vehicle\n");
1249         navit_set_vehicle(this_, this_->vehicle);
1250         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1251         if (this_->mapsets) {
1252                 ms=this_->mapsets->data;
1253                 if (this_->route) {
1254                         if ((map=route_get_map(this_->route)))
1255                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1256                         if ((map=route_get_graph_map(this_->route))) {
1257                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1258                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1259                         }
1260                         route_set_mapset(this_->route, ms);
1261                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1262                 }
1263                 if (this_->tracking) {
1264                         tracking_set_mapset(this_->tracking, ms);
1265                         if (this_->route)
1266                                 tracking_set_route(this_->tracking, this_->route);
1267                 }
1268                 if (this_->navigation) {
1269                         if ((map=navigation_get_map(this_->navigation))) {
1270                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1271                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1272                         }
1273                 }
1274                 if (this_->tracking) {
1275                         if ((map=tracking_get_map(this_->tracking))) {
1276                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1277                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1278                         }
1279                 }
1280                 navit_add_former_destinations_from_file(this_);
1281         }
1282         if (this_->route) {
1283                 struct attr callback;
1284                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1285                 callback.type=attr_callback;
1286                 callback.u.callback=this_->route_cb;
1287                 route_add_attr(this_->route, &callback);
1288         }
1289         if (this_->navigation) {
1290                 if (this_->speech) {
1291                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1292                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1293                 }
1294                 if (this_->route)
1295                         navigation_set_route(this_->navigation, this_->route);
1296         }
1297         dbg(2,"Setting Center\n");
1298         char *center_file = bookmarks_get_center_file(FALSE);
1299         bookmarks_set_center_from_file(this_->bookmarks, center_file);
1300         g_free(center_file);
1301 #if 0
1302         if (this_->menubar) {
1303                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1304                 if (men) {
1305                         navit_add_menu_windows_items(this_, men);
1306                 }
1307         }
1308 #endif
1309         global_navit=this_;
1310 #if 0
1311         navit_window_roadbook_new(this_);
1312         navit_window_items_new(this_);
1313 #endif
1314
1315         messagelist_init(this_->messages);
1316
1317         navit_set_cursors(this_);
1318
1319         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1320         callback=(this_->ready == 2);
1321         this_->ready|=1;
1322         dbg(2,"ready=%d\n",this_->ready);
1323         if (this_->ready == 3)
1324                 navit_draw(this_);
1325         if (callback)
1326                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1327 #if 0
1328         routech_test(this_);
1329 #endif
1330 }
1331
1332 void
1333 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1334 {
1335         struct coord c;
1336         int scale=16;
1337
1338         c.x=(r->rl.x+r->lu.x)/2;
1339         c.y=(r->rl.y+r->lu.y)/2;
1340         transform_set_center(this_->trans, &c);
1341         dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1342         while (scale < 1<<20) {
1343                 struct point p1,p2;
1344                 transform_set_scale(this_->trans, scale);
1345                 transform_setup_source_rect(this_->trans);
1346                 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1347                 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1348                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1349                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1350                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1351                         scale*=2;
1352                 else
1353                         break;
1354         
1355         }
1356         if (this_->ready == 3)
1357                 navit_draw_async(this_,0);
1358 }
1359
1360 void
1361 navit_zoom_to_route(struct navit *this_, int orientation)
1362 {
1363         struct map *map;
1364         struct map_rect *mr=NULL;
1365         struct item *item;
1366         struct coord c;
1367         struct coord_rect r;
1368         int count=0;
1369         if (! this_->route)
1370                 return;
1371         dbg(1,"enter\n");
1372         map=route_get_map(this_->route);
1373         dbg(1,"map=%p\n",map);
1374         if (map)
1375                 mr=map_rect_new(map, NULL);
1376         dbg(1,"mr=%p\n",mr);
1377         if (mr) {
1378                 while ((item=map_rect_get_item(mr))) {
1379                         dbg(1,"item=%s\n", item_to_name(item->type));
1380                         while (item_coord_get(item, &c, 1)) {
1381                                 dbg(1,"coord\n");
1382                                 if (!count) 
1383                                         r.lu=r.rl=c;
1384                                 else
1385                                         coord_rect_extend(&r, &c);      
1386                                 count++;
1387                         }
1388                 }
1389         }
1390         if (! count)
1391                 return;
1392         if (orientation != -1)
1393                 transform_set_yaw(this_->trans, orientation);
1394         navit_zoom_to_rect(this_, &r);
1395 }
1396
1397 static void
1398 navit_cmd_zoom_to_route(struct navit *this)
1399 {
1400         navit_zoom_to_route(this, 0);
1401 }
1402
1403
1404 /**
1405  * Change the current zoom level
1406  *
1407  * @param navit The navit instance
1408  * @param center The point where to center the map, including its projection
1409  * @returns nothing
1410  */
1411 void
1412 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1413 {
1414         struct coord *c=transform_center(this_->trans);
1415         struct coord c1,c2;
1416         enum projection pro = transform_get_projection(this_->trans);
1417         if (pro != center->pro) {
1418                 c1.x = center->x;
1419                 c1.y = center->y;
1420                 transform_from_to(&c1, center->pro, &c2, pro);
1421         } else {
1422                 c2.x = center->x;
1423                 c2.y = center->y;
1424         }
1425         *c=c2;
1426         if (set_timeout) 
1427                 navit_set_timeout(this_);
1428         if (this_->ready == 3)
1429                 navit_draw(this_);
1430 }
1431
1432 static void
1433 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1434 {
1435         int width, height;
1436         struct point po;
1437         transform_set_center(this_->trans, c);
1438         transform_get_size(this_->trans, &width, &height);
1439         po.x=width/2;
1440         po.y=height/2;
1441         update_transformation(this_->trans, &po, p, NULL);
1442         if (set_timeout)
1443                 navit_set_timeout(this_);
1444 }
1445
1446 /**
1447  * Links all vehicles to a cursor depending on the current profile.
1448  *
1449  * @param this_ A navit instance
1450  * @author Ralph Sennhauser (10/2009)
1451  */
1452 static void
1453 navit_set_cursors(struct navit *this_)
1454 {
1455         struct attr name;
1456         struct navit_vehicle *nv;
1457         struct cursor *c;
1458         GList *v;
1459
1460         v=g_list_first(this_->vehicles); // GList of navit_vehicles
1461         while (v) {
1462                 nv=v->data;
1463                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1464                         if (!strcmp(name.u.str,"none"))
1465                                 c=NULL;
1466                         else
1467                                 c=layout_get_cursor(this_->layout_current, name.u.str);
1468                 } else
1469                         c=layout_get_cursor(this_->layout_current, "default");
1470                 vehicle_set_cursor(nv->vehicle, c);
1471                 v=g_list_next(v);
1472         }
1473         return;
1474 }
1475
1476 static int
1477 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1478 {
1479         int width, height;
1480         struct navit_vehicle *nv=this_->vehicle;
1481
1482         float offset=this_->radius;      // Cursor offset from the center of the screen (percent).
1483 #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 */
1484         float min_offset = 0.;      // Percent offset at min_offset_speed.
1485         float max_offset = 30.;     // Percent offset at max_offset_speed.
1486         int min_offset_speed = 2;   // Speed in km/h
1487         int max_offset_speed = 50;  // Speed ini km/h
1488         // Calculate cursor offset from the center of the screen, upon speed.
1489         if (nv->speed <= min_offset_speed) {
1490             offset = min_offset;
1491         } else if (nv->speed > max_offset_speed) {
1492             offset = max_offset;
1493         } else {
1494             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1495         }
1496 #endif
1497
1498         transform_get_size(this_->trans, &width, &height);
1499         if (this_->orientation == -1 || keep_orientation) {
1500                 p->x=50*width/100;
1501                 p->y=(50 + offset)*height/100;
1502                 if (dir) 
1503                         *dir=keep_orientation?this_->orientation:nv->dir;
1504         } else {
1505                 int mdir;
1506                 if (this_->tracking && this_->tracking_flag) {
1507                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1508                 } else {
1509                         mdir=nv->dir-this_->orientation;
1510                 }
1511
1512                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1513                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1514                 if (dir)
1515                         *dir=this_->orientation;
1516         }
1517         return 1;
1518 }
1519
1520 void
1521 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1522 {
1523         int dir;
1524         struct point pn;
1525         struct navit_vehicle *nv=this_->vehicle;
1526         navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1527         transform_set_yaw(this_->trans, dir);
1528         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1529         if (autozoom)
1530                 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1531 }
1532
1533 static void
1534 navit_set_center_cursor_draw(struct navit *this_)
1535 {
1536         navit_set_center_cursor(this_,1,0);
1537         if (this_->ready == 3)
1538                 navit_draw_async(this_, 1);
1539 }
1540
1541 static void
1542 navit_cmd_set_center_cursor(struct navit *this_)
1543 {
1544         navit_set_center_cursor_draw(this_);
1545 }
1546
1547 void
1548 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1549 {
1550         struct coord c;
1551         struct pcoord pc;
1552         transform_reverse(this_->trans, p, &c);
1553         pc.x = c.x;
1554         pc.y = c.y;
1555         pc.pro = transform_get_projection(this_->trans);
1556         navit_set_center(this_, &pc, set_timeout);
1557 }
1558
1559 #if 0
1560                 switch((*attrs)->type) {
1561                 case attr_zoom:
1562                         zoom=(*attrs)->u.num;
1563                         break;
1564                 case attr_center:
1565                         g=*((*attrs)->u.coord_geo);
1566                         break;
1567 #endif
1568
1569 static int
1570 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1571 {
1572         int dir=0, orient_old=0, attr_updated=0;
1573         struct coord co;
1574         long zoom;
1575         GList *l;
1576         struct navit_vehicle *nv;
1577         struct attr active=(struct attr){attr_active,{(void *)0}};
1578         struct layout *lay;
1579
1580         switch (attr->type) {
1581         case attr_autozoom:
1582                 attr_updated=(this_->autozoom_secs != attr->u.num);
1583                 this_->autozoom_secs = attr->u.num;
1584                 break;
1585         case attr_autozoom_active:
1586                 attr_updated=(this_->autozoom_active != attr->u.num);
1587                 this_->autozoom_active = attr->u.num;
1588                 break;
1589         case attr_center:
1590                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1591                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1592                 transform_set_center(this_->trans, &co);
1593                 break;
1594         case attr_drag_bitmap:
1595                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1596                 this_->drag_bitmap=!!attr->u.num;
1597                 break;
1598         case attr_flags:
1599                 attr_updated=(this_->flags != attr->u.num);
1600                 this_->flags=attr->u.num;
1601                 break;
1602         case attr_flags_graphics:
1603                 attr_updated=(this_->graphics_flags != attr->u.num);
1604                 this_->graphics_flags=attr->u.num;
1605                 break;
1606         case attr_follow:
1607                 if (!this_->vehicle)
1608                         return 0;
1609                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1610                 this_->vehicle->follow_curr = attr->u.num;
1611                 break;
1612         case attr_layout:
1613                 if(this_->layout_current!=attr->u.layout) {
1614                         this_->layout_current=attr->u.layout;
1615                         graphics_font_destroy_all(this_->gra);
1616                         navit_set_cursors(this_);
1617                         if (this_->ready == 3)
1618                                 navit_draw(this_);
1619                         attr_updated=1;
1620                 }
1621                 break;
1622         case attr_layout_name:
1623                 l=this_->layouts;
1624                 while (l) {
1625                         lay=l->data;
1626                         if (!strcmp(lay->name,attr->u.str)) {
1627                                 struct attr attr;
1628                                 attr.type=attr_layout;
1629                                 attr.u.layout=lay;
1630                                 return navit_set_attr_do(this_, &attr, init);
1631                         }
1632                         l=g_list_next(l);
1633                 }               
1634                 return 0;
1635         case attr_map_border:
1636                 if (this_->border != attr->u.num) {
1637                         this_->border=attr->u.num;
1638                         attr_updated=1;
1639                 }
1640                 break;
1641         case attr_orientation:
1642                 orient_old=this_->orientation;
1643                 this_->orientation=attr->u.num;
1644                 if (!init) {
1645                         if (this_->orientation != -1) {
1646                                 dir = this_->orientation;
1647                         } else {
1648                                 if (this_->vehicle) {
1649                                         dir = this_->vehicle->dir;
1650                                 }
1651                         }
1652                         transform_set_yaw(this_->trans, dir);
1653                         if (orient_old != this_->orientation) {
1654 #if 0
1655                                 if (this_->ready == 3)
1656                                         navit_draw(this_);
1657 #endif
1658                                 attr_updated=1;
1659                         }
1660                 }
1661                 break;
1662         case attr_osd_configuration:
1663                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1664                 attr_updated=(this_->osd_configuration != attr->u.num);
1665                 this_->osd_configuration=attr->u.num;
1666                 break;
1667         case attr_pitch:
1668                 attr_updated=(this_->pitch != attr->u.num);
1669                 this_->pitch=attr->u.num;
1670                 transform_set_pitch(this_->trans, this_->pitch);
1671                 if (!init && attr_updated && this_->ready == 3)
1672                         navit_draw(this_);
1673                 break;
1674         case attr_projection:
1675                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1676                         navit_projection_set(this_, attr->u.projection, !init);
1677                         attr_updated=1;
1678                 }
1679                 break;
1680         case attr_radius:
1681                 attr_updated=(this_->radius != attr->u.num);
1682                 this_->radius=attr->u.num;
1683                 break;
1684         case attr_recent_dest:
1685                 attr_updated=(this_->recentdest_count != attr->u.num);
1686                 this_->recentdest_count=attr->u.num;
1687                 break;
1688         case attr_speech:
1689                 if(this_->speech && this_->speech != attr->u.speech) {
1690                         attr_updated=1;
1691                         this_->speech = attr->u.speech;
1692                 }
1693                 break;
1694         case attr_timeout:
1695                 attr_updated=(this_->center_timeout != attr->u.num);
1696                 this_->center_timeout = attr->u.num;
1697                 break;
1698         case attr_tracking:
1699                 attr_updated=(this_->tracking_flag != !!attr->u.num);
1700                 this_->tracking_flag=!!attr->u.num;
1701                 break;
1702         case attr_transformation:
1703                 this_->trans=attr->u.transformation;
1704                 break;
1705         case attr_use_mousewheel:
1706                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1707                 this_->use_mousewheel=!!attr->u.num;
1708                 break;
1709         case attr_vehicle:
1710                 l=this_->vehicles;
1711                 while(l) {
1712                         nv=l->data;
1713                         if (nv->vehicle == attr->u.vehicle) {
1714                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1715                                         if (this_->vehicle)
1716                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
1717                                         active.u.num=1;
1718                                         vehicle_set_attr(nv->vehicle, &active);
1719                                         attr_updated=1;
1720                                 }
1721                                 navit_set_vehicle(this_, nv);
1722                         }
1723                         l=g_list_next(l);
1724                 }
1725                 break;
1726         case attr_zoom:
1727                 zoom=transform_get_scale(this_->trans);
1728                 attr_updated=(zoom != attr->u.num);
1729                 transform_set_scale(this_->trans, attr->u.num);
1730                 if (attr_updated && !init) 
1731                         navit_draw(this_);
1732                 break;
1733         case attr_zoom_min:
1734                 attr_updated=(attr->u.num != this_->zoom_min);
1735                 this_->zoom_min=attr->u.num;
1736                 break;
1737         case attr_zoom_max:
1738                 attr_updated=(attr->u.num != this_->zoom_max);
1739                 this_->zoom_max=attr->u.num;
1740                 break;
1741         case attr_message:
1742                 navit_add_message(this_, attr->u.str);
1743                 break;
1744         case attr_follow_cursor:
1745                 attr_updated=(this_->follow_cursor != !!attr->u.num);
1746                 this_->follow_cursor=!!attr->u.num;
1747                 break;
1748         default:
1749                 return 0;
1750         }
1751         if (attr_updated && !init) {
1752                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1753                 if (attr->type == attr_osd_configuration)
1754                         graphics_draw_mode(this_->gra, draw_mode_end);
1755         }
1756         return 1;
1757 }
1758
1759 int
1760 navit_set_attr(struct navit *this_, struct attr *attr)
1761 {
1762         return navit_set_attr_do(this_, attr, 0);
1763 }
1764
1765 int
1766 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1767 {
1768         struct message *msg;
1769         int len,offset;
1770         int ret=1;
1771
1772         switch (type) {
1773         case attr_message:
1774                 msg = navit_get_messages(this_);
1775                 
1776                 if (!msg) {
1777                         return 0;
1778                 }
1779
1780                 len = 0;
1781                 while (msg) {
1782                         len += strlen(msg->text) + 1;
1783                         msg = msg->next;
1784                 }
1785                 attr->u.str = g_malloc(len + 1);
1786                 
1787                 msg = navit_get_messages(this_);
1788                 offset = 0;
1789                 while (msg) {
1790                         g_stpcpy((attr->u.str + offset), msg->text);
1791                         offset += strlen(msg->text);
1792                         attr->u.str[offset] = '\n';
1793                         offset++;
1794
1795                         msg = msg->next;
1796                 }
1797
1798                 attr->u.str[len] = '\0';
1799                 break;
1800         case attr_bookmark_map:
1801                 attr->u.map=bookmarks_get_map(this_->bookmarks);
1802                 break;
1803         case attr_bookmarks:
1804                 attr->u.bookmarks=this_->bookmarks;
1805                 break;
1806         case attr_callback_list:
1807                 attr->u.callback_list=this_->attr_cbl;
1808                 break;
1809         case attr_destination:
1810                 if (! this_->destination_valid)
1811                         return 0;
1812                 attr->u.pcoord=&this_->destination;
1813                 break;
1814         case attr_displaylist:
1815                 attr->u.displaylist=this_->displaylist;
1816                 return (attr->u.displaylist != NULL);
1817         case attr_follow:
1818                 if (!this_->vehicle)
1819                         return 0;
1820                 attr->u.num=this_->vehicle->follow_curr;
1821                 break;
1822         case attr_former_destination_map:
1823                 attr->u.map=this_->former_destination;
1824                 break;
1825         case attr_graphics:
1826                 attr->u.graphics=this_->gra;
1827                 ret=(attr->u.graphics != NULL);
1828                 break;
1829         case attr_gui:
1830                 attr->u.gui=this_->gui;
1831                 ret=(attr->u.gui != NULL);
1832                 break;
1833         case attr_layout:
1834                 if (iter) {
1835                         if (iter->u.list) {
1836                                 iter->u.list=g_list_next(iter->u.list);
1837                         } else { 
1838                                 iter->u.list=this_->layouts;
1839                         }
1840                         if (!iter->u.list)
1841                                 return 0;
1842                         attr->u.layout=(struct layout *)iter->u.list->data;
1843                 } else {
1844                         attr->u.layout=this_->layout_current;
1845                 }
1846                 break;
1847         case attr_map:
1848                 if (iter && this_->mapsets) {
1849                         if (!iter->u.mapset_handle) {
1850                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1851                         }
1852                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1853                         if(!attr->u.map) {
1854                                 mapset_close(iter->u.mapset_handle);
1855                                 return 0;
1856                         }
1857                 } else {
1858                         return 0;
1859                 }
1860                 break;
1861         case attr_mapset:
1862                 attr->u.mapset=this_->mapsets->data;
1863                 ret=(attr->u.mapset != NULL);
1864                 break;
1865         case attr_navigation:
1866                 attr->u.navigation=this_->navigation;
1867                 break;
1868         case attr_orientation:
1869                 attr->u.num=this_->orientation;
1870                 break;
1871         case attr_osd_configuration:
1872                 attr->u.num=this_->osd_configuration;
1873                 break;
1874         case attr_pitch:
1875                 attr->u.num=transform_get_pitch(this_->trans);
1876                 break;
1877         case attr_projection:
1878                 if(this_->trans) {
1879                         attr->u.num=transform_get_projection(this_->trans);
1880                 } else {
1881                         return 0;
1882                 }
1883                 break;
1884         case attr_route:
1885                 attr->u.route=this_->route;
1886                 break;
1887         case attr_speech:
1888                 attr->u.speech=this_->speech;
1889                 break;
1890         case attr_tracking:
1891                 attr->u.num=this_->tracking_flag;
1892                 break;
1893         case attr_trackingo:
1894                 attr->u.tracking=this_->tracking;
1895                 break;
1896         case attr_transformation:
1897                 attr->u.transformation=this_->trans;
1898                 break;
1899         case attr_vehicle:
1900                 if(iter) {
1901                         if(iter->u.list) {
1902                                 iter->u.list=g_list_next(iter->u.list);
1903                         } else { 
1904                                 iter->u.list=this_->vehicles;
1905                         }
1906                         if(!iter->u.list)
1907                                 return 0;
1908                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1909                 } else {
1910                         if(this_->vehicle) {
1911                                 attr->u.vehicle=this_->vehicle->vehicle;
1912                         } else {
1913                                 return 0;
1914                         }
1915                 }
1916                 break;
1917         case attr_vehicleprofile:
1918                 attr->u.vehicleprofile=this_->vehicleprofile;
1919                 break;
1920         case attr_zoom:
1921                 attr->u.num=transform_get_scale(this_->trans);
1922                 break;
1923         case attr_autozoom_active:
1924                 attr->u.num=this_->autozoom_active;
1925                 break;
1926         case attr_follow_cursor:
1927                 attr->u.num=this_->follow_cursor;
1928                 break;
1929         default:
1930                 return 0;
1931         }
1932         attr->type=type;
1933         return ret;
1934 }
1935
1936 static int
1937 navit_add_log(struct navit *this_, struct log *log)
1938 {
1939         struct attr type_attr;
1940         if (!log_get_attr(log, attr_type, &type_attr, NULL))
1941                 return 0;
1942         if (!strcmp(type_attr.u.str, "textfile_debug")) {
1943                 char *header = "type=track_tracked\n";
1944                 if (this_->textfile_debug_log)
1945                         return 0;
1946                 log_set_header(log, header, strlen(header));
1947                 this_->textfile_debug_log=log;
1948                 return 1;
1949         }
1950         return 0;
1951 }
1952
1953 int
1954 navit_add_attr(struct navit *this_, struct attr *attr)
1955 {
1956         int ret=1;
1957         switch (attr->type) {
1958         case attr_callback:
1959                 navit_add_callback(this_, attr->u.callback);
1960                 break;
1961         case attr_log:
1962                 ret=navit_add_log(this_, attr->u.log);
1963                 break;
1964         case attr_gui:
1965                 ret=navit_set_gui(this_, attr->u.gui);
1966                 break;
1967         case attr_graphics:
1968                 ret=navit_set_graphics(this_, attr->u.graphics);
1969                 break;
1970         case attr_layout:
1971                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1972                 if(!this_->layout_current) 
1973                         this_->layout_current=attr->u.layout;
1974                 break;
1975         case attr_route:
1976                 this_->route=attr->u.route;
1977                 break;
1978         case attr_mapset:
1979                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1980                 break;
1981         case attr_navigation:
1982                 this_->navigation=attr->u.navigation;
1983                 break;
1984         case attr_recent_dest:
1985                 this_->recentdest_count = attr->u.num;
1986                 break;
1987         case attr_speech:
1988                 this_->speech=attr->u.speech;
1989                 break;
1990         case attr_tracking:
1991                 this_->tracking=attr->u.tracking;
1992                 break;
1993         case attr_vehicle:
1994                 ret=navit_add_vehicle(this_, attr->u.vehicle);
1995                 break;
1996         case attr_vehicleprofile:
1997                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
1998                 break;
1999         case attr_autozoom_min:
2000                 this_->autozoom_min = attr->u.num;
2001                 break;
2002         default:
2003                 return 0;
2004         }
2005         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2006         return ret;
2007 }
2008
2009 int
2010 navit_remove_attr(struct navit *this_, struct attr *attr)
2011 {
2012         int ret=1;
2013         switch (attr->type) {
2014         case attr_callback:
2015                 navit_remove_callback(this_, attr->u.callback);
2016                 break;
2017         default:
2018                 return 0;
2019         }
2020         return ret;
2021 }
2022
2023 struct attr_iter *
2024 navit_attr_iter_new(void)
2025 {
2026         return g_new0(struct attr_iter, 1);
2027 }
2028
2029 void
2030 navit_attr_iter_destroy(struct attr_iter *iter)
2031 {
2032         g_free(iter);
2033 }
2034
2035 void
2036 navit_add_callback(struct navit *this_, struct callback *cb)
2037 {
2038         callback_list_add(this_->attr_cbl, cb);
2039 }
2040
2041 void
2042 navit_remove_callback(struct navit *this_, struct callback *cb)
2043 {
2044         callback_list_remove(this_->attr_cbl, cb);
2045 }
2046
2047 /**
2048  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2049  *
2050  * @param navit The navit instance
2051  * @returns nothing
2052  */
2053
2054 static void
2055 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2056 {
2057         struct point cursor_pnt;
2058         enum projection pro;
2059
2060         if (this_->blocked)
2061                 return;
2062         if (pnt)
2063                 cursor_pnt=*pnt;
2064         else {
2065                 pro=transform_get_projection(this_->trans_cursor);
2066                 if (!pro)
2067                         return;
2068                 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2069         }
2070         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2071 #if 0   
2072         if (pnt)
2073                 pnt2=*pnt;
2074         else {
2075                 pro=transform_get_projection(this_->trans);
2076                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2077         }
2078 #if 1
2079         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2080 #else
2081         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2082 #endif
2083 #endif
2084 }
2085
2086 static void
2087 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2088 {
2089         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2090         struct pcoord cursor_pc;
2091         struct point cursor_pnt, *pnt=&cursor_pnt;
2092         struct tracking *tracking=NULL;
2093         struct pcoord pc[16];
2094         enum projection pro=transform_get_projection(this_->trans_cursor);
2095         int count;
2096         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2097         void *attr_object;
2098         char *destination_file;
2099
2100         profile(0,NULL);
2101         if (this_->ready != 3) {
2102                 profile(0,"return 1\n");
2103                 return;
2104         }
2105         navit_layout_switch(this_);
2106         if (this_->vehicle == nv && this_->tracking_flag)
2107                 tracking=this_->tracking;
2108         if (tracking) {
2109                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2110                 attr_object=tracking;
2111                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2112         } else {
2113                 attr_object=nv->vehicle;
2114                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2115         }
2116         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2117                 if (!attr_valid.u.num != attr_position_valid_invalid)
2118                         return;
2119         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2120             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2121             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2122                 profile(0,"return 2\n");
2123                 return;
2124         }
2125         nv->dir=*attr_dir.u.numd;
2126         nv->speed=*attr_speed.u.numd;
2127         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2128         if (nv != this_->vehicle) {
2129                 navit_vehicle_draw(this_, nv, NULL);
2130                 profile(0,"return 3\n");
2131                 return;
2132         }
2133         cursor_pc.x = nv->coord.x;
2134         cursor_pc.y = nv->coord.y;
2135         cursor_pc.pro = pro;
2136         if (this_->route) {
2137                 if (tracking)
2138                         route_set_position_from_tracking(this_->route, tracking, pro);
2139                 else
2140                         route_set_position(this_->route, &cursor_pc);
2141         }
2142         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2143         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2144         if (this_->gui && nv->speed > 2)
2145                 navit_disable_suspend();
2146
2147         transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2148         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2149                 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2150                 navit_set_center_cursor_draw(this_);
2151         else
2152                 navit_vehicle_draw(this_, nv, pnt);
2153
2154         if (nv->follow_curr > 1)
2155                 nv->follow_curr--;
2156         else
2157                 nv->follow_curr=nv->follow;
2158         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2159
2160         /* Finally, if we reached our destination, stop navigation. */
2161         if (this_->route) {
2162                 switch(route_destination_reached(this_->route)) {
2163                 case 1:
2164                         route_remove_waypoint(this_->route);
2165                         count=route_get_destinations(this_->route, pc, 16);
2166                         destination_file = bookmarks_get_destination_file(TRUE);
2167                         bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2168                         break;  
2169                 case 2:
2170                         navit_set_destination(this_, NULL, NULL, 0);
2171                         break;
2172                 }
2173         }
2174         profile(0,"return 5\n");
2175 }
2176
2177 /**
2178  * Set the position of the vehicle
2179  *
2180  * @param navit The navit instance
2181  * @param c The coordinate to set as position
2182  * @returns nothing
2183  */
2184
2185 void
2186 navit_set_position(struct navit *this_, struct pcoord *c)
2187 {
2188         if (this_->route) {
2189                 route_set_position(this_->route, c);
2190                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2191         }
2192         if (this_->ready == 3)
2193                 navit_draw(this_);
2194 }
2195
2196 static int
2197 navit_set_vehicleprofile(struct navit *this_, char *name)
2198 {
2199         struct attr attr;
2200         GList *l;
2201         l=this_->vehicleprofiles;
2202         while (l) {
2203                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2204                         if (!strcmp(attr.u.str, name)) {
2205                                 this_->vehicleprofile=l->data;
2206                                 if (this_->route)
2207                                         route_set_profile(this_->route, this_->vehicleprofile);
2208                                 return 1;
2209                         }
2210                 }
2211                 l=g_list_next(l);
2212         }
2213         return 0;
2214 }
2215
2216 static void
2217 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2218 {
2219         struct attr attr;
2220         this_->vehicle=nv;
2221         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2222                 if (navit_set_vehicleprofile(this_, attr.u.str))
2223                         return;
2224         }
2225         if (!navit_set_vehicleprofile(this_,"car")) {
2226                 /* We do not have a fallback "car" profile
2227                 * so lets set any profile */
2228                 GList *l;
2229                 l=this_->vehicleprofiles;
2230                 if (l) {
2231                     this_->vehicleprofile=l->data;
2232                     if (this_->route)
2233                         route_set_profile(this_->route, this_->vehicleprofile);
2234                 }
2235         }
2236 }
2237
2238 /**
2239  * Register a new vehicle
2240  *
2241  * @param navit The navit instance
2242  * @param v The vehicle instance
2243  * @returns 1 for success
2244  */
2245 static int
2246 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2247 {
2248         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2249         struct attr follow, active, animate;
2250         nv->vehicle=v;
2251         nv->follow=0;
2252         nv->last.x = 0;
2253         nv->last.y = 0;
2254         nv->animate_cursor=0;
2255         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2256                 nv->follow=follow.u.num;
2257         nv->follow_curr=nv->follow;
2258         this_->vehicles=g_list_append(this_->vehicles, nv);
2259         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2260                 navit_set_vehicle(this_, nv);
2261         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2262                 nv->animate_cursor=animate.u.num;
2263         nv->callback.type=attr_callback;
2264         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2265         vehicle_add_attr(nv->vehicle, &nv->callback);
2266         vehicle_set_attr(nv->vehicle, &this_->self);
2267         return 1;
2268 }
2269
2270
2271
2272
2273 struct gui *
2274 navit_get_gui(struct navit *this_)
2275 {
2276         return this_->gui;
2277 }
2278
2279 struct transformation *
2280 navit_get_trans(struct navit *this_)
2281 {
2282         return this_->trans;
2283 }
2284
2285 struct route *
2286 navit_get_route(struct navit *this_)
2287 {
2288         return this_->route;
2289 }
2290
2291 struct navigation *
2292 navit_get_navigation(struct navit *this_)
2293 {
2294         return this_->navigation;
2295 }
2296
2297 struct displaylist *
2298 navit_get_displaylist(struct navit *this_)
2299 {
2300         return this_->displaylist;
2301 }
2302
2303 void
2304 navit_layout_switch(struct navit *n) 
2305 {
2306
2307     int currTs=0;
2308     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2309     double trise,tset,trise_actual;
2310     struct layout *l;
2311     int year, month, day;
2312     
2313     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2314         return; //No layout - nothing to switch
2315     }
2316     if (!n->vehicle)
2317         return;
2318     l=layout_attr.u.layout;
2319     
2320     if (l->dayname || l->nightname) {
2321         //Ok, we know that we have profile to switch
2322         
2323         //Check that we aren't calculating too fast
2324         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2325                 currTs=iso8601_to_secs(iso8601_attr.u.str);
2326                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2327         }
2328         if (currTs-(n->prevTs)<60) {
2329             //We've have to wait a little
2330             return;
2331         }
2332         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2333                 return;
2334         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2335                 return; //No valid fix yet
2336         }
2337         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2338                 //No position - no sun
2339                 return;
2340         }
2341         
2342         //We calculate sunrise anyway, cause it is needed both for day and for night
2343         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2344                 //near the pole sun never rises/sets, so we should never switch profiles
2345                 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2346                 n->prevTs=currTs;
2347                 return;
2348             }
2349         
2350         trise_actual=trise;
2351         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2352         if (l->dayname) {
2353         
2354             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
2355                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2356                 //The sun is rising now!
2357                 if (strcmp(l->name,l->dayname)) {
2358                     navit_set_layout_by_name(n,l->dayname);
2359                 }
2360             }
2361         }
2362         if (l->nightname) {
2363             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2364                 //near the pole sun never rises/sets, so we should never switch profiles
2365                 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2366                 n->prevTs=currTs;
2367                 return;
2368             }
2369             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2370             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2371                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
2372                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2373                 //Time to sleep
2374                 if (strcmp(l->name,l->nightname)) {
2375                     navit_set_layout_by_name(n,l->nightname);
2376                 }
2377             }   
2378         }
2379         
2380         n->prevTs=currTs;
2381     }
2382 }
2383
2384 int 
2385 navit_set_vehicle_by_name(struct navit *n,const char *name) 
2386 {
2387     struct vehicle *v;
2388     struct attr_iter *iter;
2389     struct attr vehicle_attr, name_attr;
2390
2391         iter=navit_attr_iter_new();
2392
2393     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2394                 v=vehicle_attr.u.vehicle;
2395                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2396                 if (name_attr.type==attr_name) {
2397                         if (!strcmp(name,name_attr.u.str)) {
2398                                 navit_set_attr(n,&vehicle_attr);                                
2399                                 navit_attr_iter_destroy(iter);
2400                                 return 1;
2401                         }
2402                 }
2403         }
2404     navit_attr_iter_destroy(iter);
2405     return 0;
2406 }
2407
2408 int 
2409 navit_set_layout_by_name(struct navit *n,const char *name) 
2410 {
2411     struct layout *l;
2412     struct attr_iter iter;
2413     struct attr layout_attr;
2414
2415     iter.u.list=0x00;
2416
2417     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2418         return 0; //No layouts - nothing to do
2419     }
2420     if (iter.u.list==NULL) {
2421         return 0;
2422     }
2423     
2424     iter.u.list=g_list_first(iter.u.list);
2425     
2426     while(iter.u.list) {
2427         l=(struct layout*)iter.u.list->data;
2428         if (!strcmp(name,l->name)) {
2429             layout_attr.u.layout=l;
2430             layout_attr.type=attr_layout;
2431             navit_set_attr(n,&layout_attr);
2432             iter.u.list=g_list_first(iter.u.list);
2433             return 1;
2434         }
2435         iter.u.list=g_list_next(iter.u.list);
2436     }
2437
2438     iter.u.list=g_list_first(iter.u.list);
2439     return 0;
2440 }
2441
2442 void
2443 navit_disable_suspend() {
2444         gui_disable_suspend(global_navit->gui);
2445         callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2446 }
2447
2448 int
2449 navit_block(struct navit *this_, int block)
2450 {
2451         if (block > 0) {
2452                 this_->blocked |= 1;
2453                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2454                         this_->blocked |= 2;
2455                 return 0;
2456         }
2457         if ((this_->blocked & 2) || block < 0) {
2458                 this_->blocked=0;
2459                 navit_draw(this_);
2460                 return 1;
2461         }
2462         this_->blocked=0;
2463         return 0;
2464 }
2465
2466 void
2467 navit_destroy(struct navit *this_)
2468 {
2469         callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2470
2471         /* TODO: destroy objects contained in this_ */
2472         if (this_->vehicle)
2473                 vehicle_destroy(this_->vehicle->vehicle);
2474         if (this_->bookmarks) {
2475                 char *center_file = bookmarks_get_center_file(TRUE);
2476                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2477                 g_free(center_file);
2478                 bookmarks_destroy(this_->bookmarks);
2479         }
2480         callback_destroy(this_->nav_speech_cb);
2481         callback_destroy(this_->roadbook_callback);
2482         callback_destroy(this_->popup_callback);
2483         callback_destroy(this_->motion_timeout_callback);
2484         if(this_->gra)
2485           graphics_remove_callback(this_->gra, this_->resize_callback);
2486         callback_destroy(this_->resize_callback);
2487         if(this_->gra)
2488           graphics_remove_callback(this_->gra, this_->button_callback);
2489         callback_destroy(this_->button_callback);
2490         if(this_->gra)
2491           graphics_remove_callback(this_->gra, this_->motion_callback);
2492         callback_destroy(this_->motion_callback);
2493         if(this_->gra)
2494           graphics_remove_callback(this_->gra, this_->predraw_callback);
2495         callback_destroy(this_->predraw_callback);
2496         route_destroy(this_->route);
2497         g_free(this_);
2498 }
2499
2500 /** @} */