Fix:Core:Made zoom_to_route work better
[navit-package] / navit / navit.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 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 "cursor.h"
46 #include "popup.h"
47 #include "data_window.h"
48 #include "route.h"
49 #include "navigation.h"
50 #include "speech.h"
51 #include "track.h"
52 #include "vehicle.h"
53 #include "color.h"
54 #include "layout.h"
55 #include "log.h"
56 #include "attr.h"
57 #include "event.h"
58 #include "file.h"
59 #include "profile.h"
60 #include "command.h"
61 #include "navit_nls.h"
62
63 /**
64  * @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
65  * @{
66  */
67
68 //! The navit_vehicule
69 struct navit_vehicle {
70         int follow;
71         /*! Limit of the follow counter. See navit_add_vehicle */
72         int follow_curr;
73         /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
74         struct coord coord;
75         int dir;
76         int speed;
77         struct coord last; /*< Position of the last update of this vehicle */
78         struct color c;
79         struct color *c2;
80         struct cursor *cursor;
81         struct vehicle *vehicle;
82         struct attr callback;
83         int animate_cursor;
84 };
85
86 struct navit {
87         struct attr self;
88         GList *mapsets;
89         GList *layouts;
90         struct gui *gui;
91         struct layout *layout_current;
92         struct graphics *gra;
93         struct action *action;
94         struct transformation *trans;
95         struct compass *compass;
96         struct route *route;
97         struct navigation *navigation;
98         struct speech *speech;
99         struct tracking *tracking;
100         int ready;
101         struct window *win;
102         struct displaylist *displaylist;
103         int cursor_flag;
104         int tracking_flag;
105         int orientation;
106         int recentdest_count;
107         int osd_configuration;
108         GList *vehicles;
109         GList *windows_items;
110         struct navit_vehicle *vehicle;
111         struct callback_list *attr_cbl;
112         int pid;
113         struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb;
114         struct datawindow *roadbook_window;
115         struct map *bookmark;
116         struct map *former_destination;
117         GHashTable *bookmarks_hash;
118         struct point pressed, last, current;
119         int button_pressed,moved,popped,zoomed;
120         time_t last_moved;
121         int center_timeout;
122         int autozoom_secs;
123         struct event_timeout *button_timeout, *motion_timeout;
124         struct callback *motion_timeout_callback;
125         int ignore_button;
126         int ignore_graphics_events;
127         struct log *textfile_debug_log;
128         struct pcoord destination;
129         int destination_valid;
130         int blocked;
131         int w,h;
132         int drag_bitmap;
133         int use_mousewheel;
134         struct callback *resize_callback,*button_callback,*motion_callback;
135 };
136
137 struct gui *main_loop_gui;
138
139 struct attr_iter {
140         union {
141                 GList *list;
142                 struct mapset_handle *mapset_handle;
143         } u;
144 };
145
146 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
147 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
148 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
149
150 void
151 navit_add_mapset(struct navit *this_, struct mapset *ms)
152 {
153         this_->mapsets = g_list_append(this_->mapsets, ms);
154 }
155
156 struct mapset *
157 navit_get_mapset(struct navit *this_)
158 {
159         if(this_->mapsets){
160                 return this_->mapsets->data;
161         } else {
162                 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
163         }
164         return NULL;
165 }
166
167 struct tracking *
168 navit_get_tracking(struct navit *this_)
169 {
170         return this_->tracking;
171 }
172
173 void
174 navit_draw_async(struct navit *this_, int async)
175 {
176         GList *l;
177         struct navit_vehicle *nv;
178
179         if (this_->blocked) {
180                 this_->blocked |= 2;
181                 return;
182         }
183         transform_setup_source_rect(this_->trans);
184         l=this_->vehicles;
185         while (l) {
186                 nv=l->data;
187                 navit_vehicle_draw(this_, nv, NULL);
188                 l=g_list_next(l);
189         }
190         graphics_draw(this_->gra, this_->displaylist, this_->mapsets, this_->trans, this_->layout_current, async, NULL);
191 }
192
193 void
194 navit_draw(struct navit *this_)
195 {
196         navit_draw_async(this_, 0);
197 }
198
199
200 void
201 navit_draw_displaylist(struct navit *this_)
202 {
203         if (this_->ready == 3)
204                 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 1);
205 }
206
207 static void
208 navit_redraw_route(struct navit *this_, int updated)
209 {
210         dbg(1,"enter %d\n", updated);
211         if (this_->ready != 3)
212                 return;
213         if (updated <= 3)
214                 return;
215         if (this_->vehicle) {
216                 if (this_->vehicle->follow == 1)
217                         return;
218                 this_->vehicle->follow_curr=this_->vehicle->follow;
219         }
220         navit_draw(this_);
221 }
222
223 void
224 navit_handle_resize(struct navit *this_, int w, int h)
225 {
226         struct map_selection sel;
227         memset(&sel, 0, sizeof(sel));
228         this_->w=w;
229         this_->h=h;
230         sel.u.p_rect.rl.x=w;
231         sel.u.p_rect.rl.y=h;
232         transform_set_screen_selection(this_->trans, &sel);
233         this_->ready |= 2;
234         graphics_set_rect(this_->gra, &sel.u.p_rect);
235         if (this_->ready == 3)
236                 navit_draw(this_);
237 }
238
239 static void
240 navit_resize(void *data, int w, int h)
241 {
242         struct navit *this=data;
243         if (!this->ignore_graphics_events)
244                 navit_handle_resize(this, w, h);
245 }
246
247 int
248 navit_get_width(struct navit *this_)
249 {
250         return this_->w;
251 }
252
253
254 int
255 navit_get_height(struct navit *this_)
256 {
257         return this_->h;
258 }
259
260 static void
261 navit_popup(void *data)
262 {
263         struct navit *this_=data;
264         popup(this_, 1, &this_->pressed);
265         this_->button_timeout=NULL;
266         this_->popped=1;
267 }
268
269
270 void
271 navit_ignore_button(struct navit *this_)
272 {
273         this_->ignore_button=1;
274 }
275
276 void
277 navit_ignore_graphics_events(struct navit *this_, int ignore)
278 {
279         this_->ignore_graphics_events=ignore;
280 }
281
282 static void
283 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
284 {
285         struct coord co,cn;
286         struct coord c,*cp;
287         int yaw;
288         double angleo,anglen;
289
290         transform_reverse(tr, old, &co);
291         if (rot) {
292                 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
293                 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
294                 yaw=transform_get_yaw(tr)+angleo-anglen;
295                 transform_set_yaw(tr, yaw % 360);
296         }
297         transform_reverse(tr, new, &cn);
298         cp=transform_get_center(tr);
299         c.x=cp->x+co.x-cn.x;
300         c.y=cp->y+co.y-cn.y;
301         dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
302         transform_set_center(tr, &c);
303 }
304
305 int
306 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
307 {
308         int border=16;
309
310         callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, (void *)pressed, (void *)button, p);
311         if (this_->ignore_button) {
312                 this_->ignore_button=0;
313                 return 0;
314         }
315         if (pressed) {
316                 this_->pressed=*p;
317                 this_->last=*p;
318                 this_->zoomed=0;
319                 if (button == 1) {
320                         this_->button_pressed=1;
321                         this_->moved=0;
322                         this_->popped=0;
323                         if (popup_callback)
324                                 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
325                 }
326                 if (button == 2)
327                         navit_set_center_screen(this_, p);
328                 if (button == 3)
329                         popup(this_, button, p);
330                 if (button == 4 && this_->use_mousewheel) {
331                         this_->zoomed = 1;
332                         navit_zoom_in(this_, 2, p);
333                 }
334                 if (button == 5 && this_->use_mousewheel) {
335                         this_->zoomed = 1;
336                         navit_zoom_out(this_, 2, p);
337                 }
338         } else {
339                 this_->button_pressed=0;
340                 if (this_->button_timeout) {
341                         event_remove_timeout(this_->button_timeout);
342                         this_->button_timeout=NULL;
343                         if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
344                                 if (!this_->zoomed) {
345                                         this_->last_moved = time(NULL);
346                                 }
347                                 navit_set_center_screen(this_, p);
348                         }
349                 }
350                 if (this_->motion_timeout) {
351                         event_remove_timeout(this_->motion_timeout);
352                         this_->motion_timeout=NULL;
353                 }
354                 if (this_->moved) {
355                         struct point pr;
356                         pr.x=this_->w/2;
357                         pr.y=this_->h;
358 #if 0
359                         update_transformation(this_->trans, &this_->pressed, p, &pr);
360 #else
361                         update_transformation(this_->trans, &this_->pressed, p, NULL);
362 #endif
363                         graphics_draw_drag(this_->gra, NULL);
364                         graphics_overlay_disable(this_->gra, 0);
365                         if (!this_->zoomed) {
366                                 this_->last_moved = time(NULL);
367                         }
368                         navit_draw(this_);
369                 } else
370                         return 1;
371         }
372         return 0;
373 }
374
375 static void
376 navit_button(void *data, int pressed, int button, struct point *p)
377 {
378         struct navit *this=data;
379         if (!this->ignore_graphics_events) {
380                 if (! this->popup_callback)
381                         this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
382                 navit_handle_button(this, pressed, button, p, this->popup_callback);
383         }
384 }
385
386
387 static void
388 navit_motion_timeout(struct navit *this_)
389 {
390         int dx, dy;
391
392         if (this_->drag_bitmap) {
393                 struct point point;
394                 point.x=(this_->current.x-this_->pressed.x);
395                 point.y=(this_->current.y-this_->pressed.y);
396                 if (graphics_draw_drag(this_->gra, &point)) {
397                         graphics_overlay_disable(this_->gra, 1);
398                         graphics_draw_mode(this_->gra, draw_mode_end);
399                         this_->moved=1;
400                         this_->motion_timeout=NULL;
401                         return;
402                 }
403         } 
404         dx=(this_->current.x-this_->last.x);
405         dy=(this_->current.y-this_->last.y);
406         if (dx || dy) {
407                 struct transformation *tr;
408                 struct point pr;
409                 this_->last=this_->current;
410                 graphics_overlay_disable(this_->gra, 1);
411                 tr=transform_dup(this_->trans);
412                 pr.x=this_->w/2;
413                 pr.y=this_->h;
414 #if 0
415                 update_transformation(tr, &this_->pressed, &this_->current, &pr);
416 #else
417                 update_transformation(tr, &this_->pressed, &this_->current, NULL);
418 #endif
419 #if 0
420                 graphics_displaylist_move(this_->displaylist, dx, dy);
421 #endif
422                 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, 0);
423                 transform_destroy(tr);
424                 this_->moved=1;
425         }
426         this_->motion_timeout=NULL;
427         return;
428 }
429
430 void
431 navit_handle_motion(struct navit *this_, struct point *p)
432 {
433         int dx, dy;
434
435         if (this_->button_pressed && !this_->popped) {
436                 dx=(p->x-this_->pressed.x);
437                 dy=(p->y-this_->pressed.y);
438                 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
439                         if (this_->button_timeout) {
440                                 event_remove_timeout(this_->button_timeout);
441                                 this_->button_timeout=NULL;
442                         }
443                         this_->current=*p;
444                         if (! this_->motion_timeout_callback)
445                                 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
446                         if (! this_->motion_timeout)
447                                 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
448                 }
449         }
450 }
451
452 static void
453 navit_motion(void *data, struct point *p)
454 {
455         struct navit *this=data;
456         if (!this->ignore_graphics_events) 
457                 navit_handle_motion(this, p);
458 }
459
460 static void
461 navit_scale(struct navit *this_, long scale, struct point *p)
462 {
463         struct coord c1, c2, *center;
464         if (p)
465                 transform_reverse(this_->trans, p, &c1);
466         transform_set_scale(this_->trans, scale);
467         if (p) {
468                 transform_reverse(this_->trans, p, &c2);
469                 center = transform_center(this_->trans);
470                 center->x += c1.x - c2.x;
471                 center->y += c1.y - c2.y;
472         }
473         navit_draw(this_);
474 }
475
476 /**
477  * @brief Automatically adjusts zoom level
478  *
479  * This function automatically adjusts the current
480  * zoom level according to the current speed.
481  *
482  * @param this_ The navit struct
483  * @param speed The vehicles speed in meters per second
484  */
485 static void
486 navit_autozoom(struct navit *this_, struct coord *center, int speed)
487 {
488         struct coord c;
489         struct point pc;
490         int distance;
491         long scale;
492         double factor;
493
494         if (this_->autozoom_secs <= 0) {
495                 return;
496         }
497
498         distance = speed * this_->autozoom_secs;
499
500         if (route_get_path_set(this_->route)) {
501                 c = route_get_coord_dist(this_->route, distance);
502         } else {
503                 return;
504         }
505
506         transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
507         factor = transform_get_autozoom_factor(this_->trans, &pc, &c);
508         
509         if ((factor < 1.1) && (factor > 0.9)) {
510                 return;
511         }
512
513         scale = transform_get_scale(this_->trans);
514         scale = (scale * factor);
515
516         navit_scale(this_, scale, &pc);
517 }
518
519 /**
520  * Change the current zoom level, zooming closer to the ground
521  *
522  * @param navit The navit instance
523  * @param factor The zoom factor, usually 2
524  * @param p The invariant point (if set to NULL, default to center)
525  * @returns nothing
526  */
527 void
528 navit_zoom_in(struct navit *this_, int factor, struct point *p)
529 {
530         long scale=transform_get_scale(this_->trans)/factor;
531         if (scale < 1)
532                 scale=1;
533         navit_scale(this_, scale, p);
534 }
535
536 /**
537  * Change the current zoom level
538  *
539  * @param navit The navit instance
540  * @param factor The zoom factor, usually 2
541  * @param p The invariant point (if set to NULL, default to center)
542  * @returns nothing
543  */
544 void
545 navit_zoom_out(struct navit *this_, int factor, struct point *p)
546 {
547         long scale=transform_get_scale(this_->trans)*factor;
548         navit_scale(this_, scale, p);
549 }
550
551 static int
552 navit_cmd_zoom_in(struct navit *this_)
553 {
554         navit_zoom_in(this_, 2, NULL);
555         return 0;
556 }
557
558 static int
559 navit_cmd_zoom_out(struct navit *this_)
560 {
561         navit_zoom_out(this_, 2, NULL);
562         return 0;
563 }
564
565 static struct command_table commands[] = {
566         {"zoom_in",navit_cmd_zoom_in},
567         {"zoom_out",navit_cmd_zoom_out},
568 };
569         
570
571 struct navit *
572 navit_new(struct attr *parent, struct attr **attrs)
573 {
574         struct navit *this_=g_new0(struct navit, 1);
575         struct pcoord center;
576         struct coord co;
577         struct coord_geo g;
578         enum projection pro=projection_mg;
579         int zoom = 256;
580         FILE *f;
581         g.lat=53.13;
582         g.lng=11.70;
583
584         this_->self.type=attr_navit;
585         this_->self.u.navit=this_;
586         this_->attr_cbl=callback_list_new();
587         main_add_navit(this_);
588
589 #if !defined(_WIN32) && !defined(__CEGCC__)
590         f=popen("pidof /usr/bin/ipaq-sleep","r");
591         if (f) {
592                 fscanf(f,"%d",&this_->pid);
593                 dbg(1,"ipaq_sleep pid=%d\n", this_->pid);
594                 pclose(f);
595         }
596 #endif
597
598         this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
599
600         this_->cursor_flag=1;
601         this_->orientation=-1;
602         this_->tracking_flag=1;
603         this_->recentdest_count=10;
604         this_->osd_configuration=-1;
605
606         this_->last_moved = 0;
607         this_->center_timeout = 10;
608         this_->use_mousewheel = 1;
609         this_->autozoom_secs = 0;
610
611         for (;*attrs; attrs++) {
612                 switch((*attrs)->type) {
613                 case attr_zoom:
614                         zoom=(*attrs)->u.num;
615                         break;
616                 case attr_center:
617                         g=*((*attrs)->u.coord_geo);
618                         break;
619                 case attr_cursor:
620                         this_->cursor_flag=!!(*attrs)->u.num;
621                         break;
622                 case attr_orientation:
623                         this_->orientation=(*attrs)->u.num;
624                         break;
625                 case attr_osd_configuration:
626                         this_->osd_configuration=(*attrs)->u.num;
627                         break;
628                 case attr_tracking:
629                         this_->tracking_flag=!!(*attrs)->u.num;
630                         break;
631                 case attr_recent_dest:
632                         this_->recentdest_count=(*attrs)->u.num;
633                         break;
634                 case attr_drag_bitmap:
635                         this_->drag_bitmap=!!(*attrs)->u.num;
636                         break;
637                 case attr_use_mousewheel:
638                         this_->use_mousewheel=!!(*attrs)->u.num;
639                         break;
640                 case attr_timeout:
641                         this_->center_timeout = (*attrs)->u.num;
642                         break;
643                 case attr_autozoom:
644                         this_->autozoom_secs = (*attrs)->u.num;
645                 default:
646                         dbg(0, "Unexpected attribute %x\n",(*attrs)->type);
647                         break;
648                 }
649         }
650         transform_from_geo(pro, &g, &co);
651         center.x=co.x;
652         center.y=co.y;
653         center.pro = pro;
654
655         this_->trans=transform_new();
656         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
657         this_->displaylist=graphics_displaylist_new();
658         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
659         return this_;
660 }
661
662 static int
663 navit_set_gui(struct navit *this_, struct gui *gui)
664 {
665         if (this_->gui)
666                 return 0;
667         this_->gui=gui;
668         if (gui_has_main_loop(this_->gui)) {
669                 if (! main_loop_gui) {
670                         main_loop_gui=this_->gui;
671                 } else {
672                         dbg(0,"gui with main loop already active, ignoring this instance");
673                         return 0;
674                 }
675         }
676         return 1;
677 }
678
679 static int
680 navit_set_graphics(struct navit *this_, struct graphics *gra)
681 {
682         if (this_->gra)
683                 return 0;
684         this_->gra=gra;
685         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
686         graphics_add_callback(gra, this_->resize_callback);
687         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
688         graphics_add_callback(gra, this_->button_callback);
689         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
690         graphics_add_callback(gra, this_->motion_callback);
691         return 1;
692 }
693
694 struct graphics *
695 navit_get_graphics(struct navit *this_)
696 {
697         return this_->gra;
698 }
699
700 static void
701 navit_projection_set(struct navit *this_, enum projection pro)
702 {
703         struct coord_geo g;
704         struct coord *c;
705
706         c=transform_center(this_->trans);
707         transform_to_geo(transform_get_projection(this_->trans), c, &g);
708         transform_set_projection(this_->trans, pro);
709         transform_from_geo(pro, &g, c);
710         navit_draw(this_);
711 }
712
713 /**
714  * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
715  */
716 static void
717 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
718 {
719         FILE *f;
720         int offset=0;
721         char *buffer;
722         int ch,prev,lines=0;
723         int numc,readc;
724         int fd;
725         const char *prostr;
726
727         f=fopen(file, "r");
728         if (!f)
729                 goto new_file;
730         if (limit != 0) {
731                 prev = '\n';
732                 while ((ch = fgetc(f)) != EOF) {
733                         if ((ch == '\n') && (prev != '\n')) {
734                                 lines++;
735                         }
736                         prev = ch;
737                 }
738
739                 if (prev != '\n') { // Last line did not end with a newline
740                         lines++;
741                 }
742
743                 fclose(f);
744                 f = fopen(file, "r+");
745                 fd = fileno(f);
746                 while (lines >= limit) { // We have to "scroll up"
747                         rewind(f);
748                         numc = 0; // Counts how many bytes we have in our line to scroll up
749                         while ((ch = fgetc(f)) != EOF) {
750                                 numc++;
751                                 if (ch == '\n') {
752                                         break;
753                                 }
754                         }
755
756                         buffer=g_malloc(numc);
757                         offset = numc; // Offset holds where we currently are
758                         
759                         do {
760                                 fseek(f,offset,SEEK_SET);
761                                 readc = fread(buffer,1,numc,f);
762                                 
763                                 fseek(f,-(numc+readc),SEEK_CUR);
764                                 fwrite(buffer,1,readc,f);
765
766                                 offset += readc;
767                         } while (readc == numc);
768
769                         g_free(buffer);
770                         fflush(f);
771                         ftruncate(fd,(offset-numc));
772 #ifdef HAVE_FSYNC
773                         fsync(fd);
774 #endif
775
776                         lines--;
777                 }
778                 fclose(f);
779         }
780
781 new_file:
782         f=fopen(file, "a");
783         if (f) {
784                 if (c) {
785                         prostr = projection_to_name(c->pro);
786                         fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
787                                  prostr, *prostr ? ":" : "", 
788                                  c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x, 
789                                  c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y, 
790                                  type, description);
791                 } else
792                         fprintf(f,"\n");
793                 fclose(f);
794         }
795 }
796
797 /*
798  * navit_get_user_data_directory
799  * 
800  * returns the directory used to store user data files (center.txt,
801  * destination.txt, bookmark.txt, ...)
802  *
803  * arg: gboolean create: create the directory if it does not exist
804  */
805 static char*
806 navit_get_user_data_directory(gboolean create) {
807         char *dir;
808         dir = getenv("NAVIT_USER_DATADIR");
809         if (create && !file_exists(dir)) {
810                 dbg(0,"creating dir %s\n", dir);
811                 if (file_mkdir(dir,0)) {
812                         dbg(0,"failed creating dir %s\n", dir);
813                         return NULL;
814                 }
815         }
816
817         return dir;
818 }
819
820 /*
821  * navit_get_destination_file
822  * 
823  * returns the name of the file used to store destinations with its
824  * full path
825  *
826  * arg: gboolean create: create the directory where the file is stored
827  * if it does not exist
828  */
829 static char*
830 navit_get_destination_file(gboolean create)
831 {
832         return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
833 }
834
835 /*
836  * navit_get_bookmark_file
837  * 
838  * returns the name of the file used to store bookmarks with its
839  * full path
840  *
841  * arg: gboolean create: create the directory where the file is stored
842  * if it does not exist
843  */
844 static char*
845 navit_get_bookmark_file(gboolean create)
846 {
847         return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
848 }
849
850
851 /*
852  * navit_get_bookmark_file
853  * 
854  * returns the name of the file used to store the center file  with its
855  * full path
856  *
857  * arg: gboolean create: create the directory where the file is stored
858  * if it does not exist
859  */
860 static char*
861 navit_get_center_file(gboolean create)
862 {
863         return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
864 }
865
866 static void
867 navit_set_center_from_file(struct navit *this_, char *file)
868 {
869         FILE *f;
870         char *line = NULL;
871
872         size_t line_size = 0;
873         enum projection pro;
874         struct coord *center;
875
876         f = fopen(file, "r");
877         if (! f)
878                 return;
879         getline(&line, &line_size, f);
880         fclose(f);
881         if (line) {
882                 center = transform_center(this_->trans);
883                 pro = transform_get_projection(this_->trans);
884                 coord_parse(g_strchomp(line), pro, center);
885                 free(line);
886         }
887         return;
888 }
889  
890 static void
891 navit_write_center_to_file(struct navit *this_, char *file)
892 {
893         FILE *f;
894         enum projection pro;
895         struct coord *center;
896
897         f = fopen(file, "w+");
898         if (f) {
899                 center = transform_center(this_->trans);
900                 pro = transform_get_projection(this_->trans);
901                 coord_print(pro, center, f);
902                 fclose(f);
903         } else {
904                 perror(file);
905         }
906         return;
907 }
908
909
910 /**
911  * Start the route computing to a given set of coordinates
912  *
913  * @param navit The navit instance
914  * @param c The coordinate to start routing to
915  * @param description A label which allows the user to later identify this destination in the former destinations selection
916  * @returns nothing
917  */
918 void
919 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description)
920 {
921         if (c) {
922                 this_->destination=*c;
923                 this_->destination_valid=1;
924         } else
925                 this_->destination_valid=0;
926         char *destination_file = navit_get_destination_file(TRUE);
927         navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
928         g_free(destination_file);
929         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
930         if (this_->route) {
931                 route_set_destination(this_->route, c);
932
933                 if (this_->ready == 3)
934                         navit_draw(this_);
935         }
936 }
937
938 /**
939  * @brief Checks if a route is calculated
940  *
941  * This function checks if a route is calculated.
942  *
943  * @param this_ The navit struct whose route should be checked.
944  * @return True if the route is set, false otherwise.
945  */
946 int
947 navit_check_route(struct navit *this_)
948 {
949         if (this_->route) {
950                 return route_get_path_set(this_->route);
951         }
952
953         return 0;
954 }
955
956 /**
957  * Record the given set of coordinates as a bookmark
958  *
959  * @param navit The navit instance
960  * @param c The coordinate to store
961  * @param description A label which allows the user to later identify this bookmark
962  * @returns nothing
963  */
964 void
965 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
966 {
967         char *bookmark_file = navit_get_bookmark_file(TRUE);
968         navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
969         g_free(bookmark_file);
970
971         callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
972 }
973
974 struct navit *global_navit;
975
976 static void
977 navit_add_bookmarks_from_file(struct navit *this_)
978 {
979         char *bookmark_file = navit_get_bookmark_file(FALSE);
980         struct attr parent={attr_navit, .u.navit=this_};
981         struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
982         struct attr *attrs[]={&type, &data, NULL};
983
984         this_->bookmark=map_new(&parent, attrs);
985         g_free(bookmark_file);
986 }
987
988 static int
989 navit_former_destinations_active(struct navit *this_)
990 {
991         char *destination_file = navit_get_destination_file(FALSE);
992         FILE *f;
993         int active=0;
994         char buffer[3];
995         f=fopen(destination_file,"r");
996         if (f) {
997                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
998                         active=1;
999                 fclose(f);
1000         }
1001         g_free(destination_file);
1002         return active;
1003 }
1004
1005 static void
1006 navit_add_former_destinations_from_file(struct navit *this_)
1007 {
1008         char *destination_file = navit_get_destination_file(FALSE);
1009         struct attr parent={attr_navit, .u.navit=this_};
1010         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
1011         struct attr *attrs[]={&type, &data, NULL};
1012         struct map_rect *mr;
1013         struct item *item;
1014         int valid=0;
1015         struct coord c;
1016         struct pcoord pc;
1017
1018         this_->former_destination=map_new(&parent, attrs);
1019         g_free(destination_file);
1020         if (!this_->route || !navit_former_destinations_active(this_))
1021                 return; 
1022         mr=map_rect_new(this_->former_destination, NULL);
1023         while ((item=map_rect_get_item(mr))) {
1024                 if (item->type == type_former_destination && item_coord_get(item, &c, 1)) 
1025                         valid=1;
1026         }
1027         map_rect_destroy(mr);
1028         pc.pro=map_projection(this_->former_destination);
1029         pc.x=c.x;
1030         pc.y=c.y;
1031         if (valid) {
1032                 route_set_destination(this_->route, &pc);
1033                 this_->destination=pc;
1034                 this_->destination_valid=1;
1035         }
1036 }
1037
1038
1039 static void
1040 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1041 {
1042         va_list ap;
1043         char *str1,*str2;
1044         va_start(ap, fmt);
1045         if (this_->textfile_debug_log && this_->vehicle) {
1046                 str1=g_strdup_vprintf(fmt, ap);
1047                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1048                 log_write(this_->textfile_debug_log, str2, strlen(str2));
1049                 g_free(str2);
1050                 g_free(str1);
1051         }
1052         va_end(ap);
1053 }
1054
1055 int 
1056 navit_speech_estimate(struct navit *this_, char *str)
1057 {
1058         return speech_estimate_duration(this_->speech, str);
1059 }
1060
1061 void
1062 navit_say(struct navit *this_, char *text)
1063 {
1064         speech_say(this_->speech, text);
1065 }
1066
1067 /**
1068  * @brief Toggles the navigation announcer for navit
1069  * @param this_ The navit object
1070  */
1071 void
1072 navit_announcer_toggle(struct navit *this_)
1073 {
1074     struct attr attr, speechattr;
1075
1076     // search for the speech attribute
1077     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1078         return;
1079     // find out if the corresponding attribute attr_active has been set
1080     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1081         // flip it then...
1082         attr.u.num = !attr.u.num;
1083     } else {
1084         // otherwise disable it because voice is enabled by default
1085         attr.type = attr_active;
1086         attr.u.num = 0;
1087     }
1088
1089     // apply the new state
1090     if(!speech_set_attr(speechattr.u.speech, &attr))
1091         return;
1092
1093     // announce that the speech attribute has changed
1094     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1095 }
1096
1097 void
1098 navit_speak(struct navit *this_)
1099 {
1100         struct navigation *nav=this_->navigation;
1101         struct map *map=NULL;
1102         struct map_rect *mr=NULL;
1103         struct item *item;
1104         struct attr attr;
1105
1106     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1107         attr.u.num = 1;
1108     dbg(1, "this_.speech->active %i\n", attr.u.num);
1109     if(!attr.u.num)
1110         return;
1111
1112         if (nav)
1113                 map=navigation_get_map(nav);
1114         if (map)
1115                 mr=map_rect_new(map, NULL);
1116         if (mr) {
1117                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1118                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1119                         speech_say(this_->speech, attr.u.str);
1120                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1121                 }
1122                 map_rect_destroy(mr);
1123         }
1124 }
1125
1126 static void
1127 navit_window_roadbook_update(struct navit *this_)
1128 {
1129         struct navigation *nav=this_->navigation;
1130         struct map *map=NULL;
1131         struct map_rect *mr=NULL;
1132         struct item *item;
1133         struct attr attr;
1134         struct param_list param[5];
1135         int secs;
1136
1137         dbg(1,"enter\n");
1138         datawindow_mode(this_->roadbook_window, 1);
1139         if (nav)
1140                 map=navigation_get_map(nav);
1141         if (map)
1142                 mr=map_rect_new(map, NULL);
1143         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1144         if (mr) {
1145                 dbg(0,"while loop\n");
1146                 while ((item=map_rect_get_item(mr))) {
1147                         dbg(0,"item=%p\n", item);
1148                         attr.u.str=NULL;
1149                         if (item->type != type_nav_position) {
1150                                 item_attr_get(item, attr_navigation_long, &attr);
1151                                 dbg(2, "Command='%s'\n", attr.u.str);
1152                                 param[0].value=g_strdup(attr.u.str);
1153                         } else
1154                                 param[0].value=_("Position");
1155                         param[0].name=_("Command");
1156
1157                         item_attr_get(item, attr_length, &attr);
1158                         dbg(2, "Length=%d\n", attr.u.num);
1159                         param[1].name=_("Length");
1160
1161                         if ( attr.u.num >= 2000 )
1162                         {
1163                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1164                         }
1165                         else
1166                         {
1167                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1168                         }
1169
1170                         item_attr_get(item, attr_time, &attr);
1171                         dbg(2, "Time=%d\n", attr.u.num);
1172                         secs=attr.u.num/10;
1173                         param[2].name=_("Time");
1174                         if ( secs >= 3600 )
1175                         {
1176                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1177                         }
1178                         else
1179                         {
1180                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1181                         }
1182
1183                         item_attr_get(item, attr_destination_length, &attr);
1184                         dbg(2, "Destlength=%d\n", attr.u.num);
1185                         param[3].name=_("Destination Length");
1186                         if ( attr.u.num >= 2000 )
1187                         {
1188                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1189                         }
1190                         else
1191                         {
1192                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1193                         }
1194
1195                         item_attr_get(item, attr_destination_time, &attr);
1196                         dbg(2, "Desttime=%d\n", attr.u.num);
1197                         secs=attr.u.num/10;
1198                         param[4].name=_("Destination Time");
1199                         if ( secs >= 3600 )
1200                         {
1201                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1202                         }
1203                         else
1204                         {
1205                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1206                         }
1207                         datawindow_add(this_->roadbook_window, param, 5);
1208                 }
1209                 map_rect_destroy(mr);
1210         }
1211         datawindow_mode(this_->roadbook_window, 0);
1212 }
1213
1214 void
1215 navit_window_roadbook_destroy(struct navit *this_)
1216 {
1217         dbg(0, "enter\n");
1218         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1219         this_->roadbook_window=NULL;
1220         this_->roadbook_callback=NULL;
1221 }
1222 void
1223 navit_window_roadbook_new(struct navit *this_)
1224 {
1225         if (this_->roadbook_callback || this_->roadbook_window) {
1226                 return;
1227         }
1228
1229         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1230         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1231         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1232         navit_window_roadbook_update(this_);
1233 }
1234
1235 void
1236 navit_init(struct navit *this_)
1237 {
1238         struct mapset *ms;
1239         struct map *map;
1240
1241         if (!this_->gui) {
1242                 dbg(0,"no gui\n");
1243                 navit_destroy(this_);
1244                 return;
1245         }
1246         if (!this_->gra) {
1247                 dbg(0,"no graphics\n");
1248                 navit_destroy(this_);
1249                 return;
1250         }
1251         if (gui_set_graphics(this_->gui, this_->gra)) {
1252                 struct attr attr_type_gui, attr_type_graphics;
1253                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1254                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1255                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1256                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1257                 dbg(0," for explanations and solutions\n");
1258
1259                 navit_destroy(this_);
1260                 return;
1261         }
1262         graphics_init(this_->gra);
1263         if (this_->mapsets) {
1264                 ms=this_->mapsets->data;
1265                 if (this_->route) {
1266                         if ((map=route_get_map(this_->route)))
1267                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1268                         if ((map=route_get_graph_map(this_->route))) {
1269                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1270                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1271                         }
1272                         route_set_mapset(this_->route, ms);
1273                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1274                 }
1275                 if (this_->tracking) {
1276                         tracking_set_mapset(this_->tracking, ms);
1277                         if (this_->route)
1278                                 tracking_set_route(this_->tracking, this_->route);
1279                 }
1280                 if (this_->navigation) {
1281                         if ((map=navigation_get_map(this_->navigation))) {
1282                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1283                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1284                         }
1285                 }
1286                 if (this_->tracking) {
1287                         if ((map=tracking_get_map(this_->tracking))) {
1288                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1289                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1290                         }
1291                 }
1292                 navit_add_bookmarks_from_file(this_);
1293                 navit_add_former_destinations_from_file(this_);
1294         }
1295         if (this_->route) {
1296                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route, this_);
1297                 route_add_callback(this_->route, this_->route_cb);
1298         }
1299         if (this_->navigation) {
1300                 if (this_->speech) {
1301                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1302                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1303                 }
1304                 if (this_->route)
1305                         navigation_set_route(this_->navigation, this_->route);
1306         }
1307         char *center_file = navit_get_center_file(FALSE);
1308         navit_set_center_from_file(this_, center_file);
1309         g_free(center_file);
1310 #if 0
1311         if (this_->menubar) {
1312                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1313                 if (men) {
1314                         navit_add_menu_windows_items(this_, men);
1315                 }
1316         }
1317 #endif
1318         global_navit=this_;
1319 #if 0
1320         navit_window_roadbook_new(this_);
1321         navit_window_items_new(this_);
1322 #endif
1323         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1324         this_->ready|=1;
1325         if (this_->ready == 3)
1326                 navit_draw(this_);
1327 }
1328
1329 void
1330 navit_zoom_to_route(struct navit *this_)
1331 {
1332         struct map *map;
1333         struct map_rect *mr=NULL;
1334         struct item *item;
1335         struct coord c,*ct;
1336         struct coord_rect r;
1337         int count=0,scale=16;
1338         if (! this_->route)
1339                 return;
1340         dbg(0,"enter\n");
1341         map=route_get_map(this_->route);
1342         dbg(0,"map=%p\n",map);
1343         if (map)
1344                 mr=map_rect_new(map, NULL);
1345         dbg(0,"mr=%p\n",mr);
1346         if (mr) {
1347                 while ((item=map_rect_get_item(mr))) {
1348                         dbg(0,"item=%s\n", item_to_name(item->type));
1349                         while (item_coord_get(item, &c, 1)) {
1350                                 dbg(0,"coord\n");
1351                                 if (!count) 
1352                                         r.lu=r.rl=c;
1353                                 else
1354                                         coord_rect_extend(&r, &c);      
1355                                 count++;
1356                         }
1357                 }
1358         }
1359         if (! count)
1360                 return;
1361         c.x=(r.rl.x+r.lu.x)/2;
1362         c.y=(r.rl.y+r.lu.y)/2;
1363         dbg(0,"count=%d\n",count);
1364         ct=transform_center(this_->trans);
1365         *ct=c;
1366         dbg(0,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1367         while (scale < 1<<20) {
1368                 struct point p1,p2;
1369                 transform_set_scale(this_->trans, scale);
1370                 transform_setup_source_rect(this_->trans);
1371                 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1372                 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1373                 dbg(0,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1374                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1375                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1376                         scale*=2;
1377                 else
1378                         break;
1379         
1380         }
1381         if (this_->ready == 3)
1382                 navit_draw(this_);
1383 }
1384
1385 /**
1386  * Change the current zoom level
1387  *
1388  * @param navit The navit instance
1389  * @param center The point where to center the map, including its projection
1390  * @returns nothing
1391  */
1392 void
1393 navit_set_center(struct navit *this_, struct pcoord *center)
1394 {
1395         struct coord *c=transform_center(this_->trans);
1396         struct coord c1,c2;
1397         enum projection pro = transform_get_projection(this_->trans);
1398         if (pro != center->pro) {
1399                 c1.x = center->x;
1400                 c1.y = center->y;
1401                 transform_from_to(&c1, center->pro, &c2, pro);
1402         } else {
1403                 c2.x = center->x;
1404                 c2.y = center->y;
1405         }
1406         *c=c2;
1407         if (this_->ready == 3)
1408                 navit_draw(this_);
1409 }
1410
1411 static void
1412 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p)
1413 {
1414         int width, height;
1415         struct point po;
1416         transform_set_center(this_->trans, c);
1417         transform_get_size(this_->trans, &width, &height);
1418         po.x=width/2;
1419         po.y=height/2;
1420         update_transformation(this_->trans, &po, p, NULL);
1421 }
1422
1423 static void
1424 navit_set_center_cursor(struct navit *this_, struct coord *cursor, int dir, int xpercent, int ypercent)
1425 {
1426         int width, height;
1427         struct point pn;
1428
1429         transform_get_size(this_->trans, &width, &height);
1430         transform_set_yaw(this_->trans, dir);
1431         pn.x=xpercent*width/100;
1432         pn.y=ypercent*height/100;
1433         navit_set_center_coord_screen(this_, cursor, &pn);
1434         if (this_->ready == 3)
1435                 navit_draw_async(this_, 1);
1436 }
1437
1438
1439 void
1440 navit_set_center_screen(struct navit *this_, struct point *p)
1441 {
1442         struct coord c;
1443         struct pcoord pc;
1444         transform_reverse(this_->trans, p, &c);
1445         pc.x = c.x;
1446         pc.y = c.y;
1447         pc.pro = transform_get_projection(this_->trans);
1448         navit_set_center(this_, &pc);
1449 }
1450
1451 int
1452 navit_set_attr(struct navit *this_, struct attr *attr)
1453 {
1454         int dir=0, orient_old=0, attr_updated=0;
1455
1456         switch (attr->type) {
1457         case attr_cursor:
1458                 if (this_->cursor_flag != !!attr->u.num) {
1459                         this_->cursor_flag=!!attr->u.num;
1460                         attr_updated=1;
1461                 }
1462                 break;
1463         case attr_layout:
1464                 if(this_->layout_current!=attr->u.layout) {
1465                         this_->layout_current=attr->u.layout;
1466                         graphics_font_destroy_all(this_->gra);
1467                         navit_draw(this_);
1468                         attr_updated=1;
1469                 }
1470                 break;
1471         case attr_orientation:
1472                 orient_old=this_->orientation;
1473                 this_->orientation=attr->u.num;
1474                 if (this_->orientation != -1) {
1475                         dir = this_->orientation;
1476                 } else {
1477                         if (this_->vehicle) {
1478                                 dir = this_->vehicle->dir;
1479                         }
1480                 }
1481                 transform_set_yaw(this_->trans, dir);
1482                 if (orient_old != this_->orientation) {
1483                         if (this_->ready == 3)
1484                                 navit_draw(this_);
1485                         attr_updated=1;
1486                 }
1487                 break;
1488         case attr_osd_configuration:
1489                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1490                 if (this_->osd_configuration != attr->u.num)
1491                         attr_updated=1;
1492                 this_->osd_configuration=attr->u.num;
1493                 break;
1494         case attr_projection:
1495                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1496                         navit_projection_set(this_, attr->u.projection);
1497                         attr_updated=1;
1498                 }
1499                 break;
1500     case attr_speech:
1501         if(this_->speech && this_->speech != attr->u.speech) {
1502             attr_updated=1;
1503             this_->speech = attr->u.speech;
1504         }
1505         break;
1506         case attr_tracking:
1507                 if (this_->tracking_flag != !!attr->u.num) {
1508                         this_->tracking_flag=!!attr->u.num;
1509                         attr_updated=1;
1510                 }
1511                 break;
1512         case attr_vehicle:
1513                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1514                         GList *l;
1515                         struct navit_vehicle *nv;
1516                         struct attr active=(struct attr){attr_active,{(void *)0}};
1517                         l=this_->vehicles;
1518                         while(l) {
1519                                 nv=l->data;
1520                                 if (nv->vehicle == attr->u.vehicle) {
1521                                         if (this_->vehicle)
1522                                                 vehicle_set_attr(this_->vehicle->vehicle, &active, NULL);
1523                                         active.u.num=1;
1524                                         vehicle_set_attr(nv->vehicle, &active, NULL);
1525                                         this_->vehicle=nv;
1526                                         attr_updated=1;
1527                                 }
1528                                 l=g_list_next(l);
1529                         }
1530                 }
1531                 break;
1532         default:
1533                 return 0;
1534         }
1535         if (attr_updated) {
1536                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1537                 if (attr->type == attr_osd_configuration)
1538                         graphics_draw_mode(this_->gra, draw_mode_end);
1539         }
1540         return 1;
1541 }
1542
1543 int
1544 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1545 {
1546         switch (type) {
1547         case attr_bookmark_map:
1548                 attr->u.map=this_->bookmark;
1549                 break;
1550         case attr_callback_list:
1551                 attr->u.callback_list=this_->attr_cbl;
1552                 break;
1553         case attr_cursor:
1554                 attr->u.num=this_->cursor_flag;
1555                 break;
1556         case attr_destination:
1557                 if (! this_->destination_valid)
1558                         return 0;
1559                 attr->u.pcoord=&this_->destination;
1560                 break;
1561         case attr_former_destination_map:
1562                 attr->u.map=this_->former_destination;
1563                 break;
1564         case attr_gui:
1565                 attr->u.gui=this_->gui;
1566                 break;
1567         case attr_layout:
1568                 if (iter) {
1569                         if (iter->u.list) {
1570                                 iter->u.list=g_list_next(iter->u.list);
1571                         } else { 
1572                                 iter->u.list=this_->layouts;
1573                         }
1574                         if (!iter->u.list)
1575                                 return 0;
1576                         attr->u.layout=(struct layout *)iter->u.list->data;
1577                 } else {
1578                         attr->u.layout=this_->layout_current;
1579                 }
1580                 break;
1581         case attr_map:
1582                 if (iter && this_->mapsets) {
1583                         if (!iter->u.mapset_handle) {
1584                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1585                         }
1586                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1587                         if(!attr->u.map) {
1588                                 mapset_close(iter->u.mapset_handle);
1589                                 return 0;
1590                         }
1591                 } else {
1592                         return 0;
1593                 }
1594                 break;
1595         case attr_navigation:
1596                 attr->u.navigation=this_->navigation;
1597                 break;
1598         case attr_orientation:
1599                 attr->u.num=this_->orientation;
1600                 break;
1601         case attr_osd_configuration:
1602                 attr->u.num=this_->osd_configuration;
1603                 break;
1604         case attr_projection:
1605                 if(this_->trans) {
1606                         attr->u.num=transform_get_projection(this_->trans);
1607                 } else {
1608                         return 0;
1609                 }
1610                 break;
1611         case attr_route:
1612                 attr->u.route=this_->route;
1613                 break;
1614         case attr_speech:
1615                 attr->u.speech=this_->speech;
1616                 break;
1617         case attr_tracking:
1618                 attr->u.num=this_->tracking_flag;
1619                 break;
1620         case attr_vehicle:
1621                 if(iter) {
1622                         if(iter->u.list) {
1623                                 iter->u.list=g_list_next(iter->u.list);
1624                         } else { 
1625                                 iter->u.list=this_->vehicles;
1626                         }
1627                         if(!iter->u.list)
1628                                 return 0;
1629                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1630                 } else {
1631                         if(this_->vehicle) {
1632                                 attr->u.vehicle=this_->vehicle->vehicle;
1633                         } else {
1634                                 return 0;
1635                         }
1636                 }
1637                 break;
1638         case attr_zoom:
1639                 attr->u.num=transform_get_scale(this_->trans);
1640                 break;
1641         default:
1642                 return 0;
1643         }
1644         attr->type=type;
1645         return 1;
1646 }
1647
1648 static int
1649 navit_add_log(struct navit *this_, struct log *log)
1650 {
1651         struct attr type_attr;
1652         if (!log_get_attr(log, attr_type, &type_attr, NULL))
1653                 return 0;
1654         if (!strcmp(type_attr.u.str, "textfile_debug")) {
1655                 char *header = "type=track_tracked\n";
1656                 if (this_->textfile_debug_log)
1657                         return 0;
1658                 log_set_header(log, header, strlen(header));
1659                 this_->textfile_debug_log=log;
1660                 return 1;
1661         }
1662         return 0;
1663 }
1664
1665 int
1666 navit_add_attr(struct navit *this_, struct attr *attr)
1667 {
1668         int ret=1;
1669         switch (attr->type) {
1670         case attr_log:
1671                 ret=navit_add_log(this_, attr->u.log);
1672                 break;
1673         case attr_gui:
1674                 ret=navit_set_gui(this_, attr->u.gui);
1675                 break;
1676         case attr_graphics:
1677                 ret=navit_set_graphics(this_, attr->u.graphics);
1678                 break;
1679         case attr_layout:
1680                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1681                 if(!this_->layout_current) 
1682                         this_->layout_current=attr->u.layout;
1683                 ret=1;
1684                 break;
1685         case attr_route:
1686                 this_->route=attr->u.route;
1687                 break;
1688         case attr_mapset:
1689                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1690                 break;
1691         case attr_navigation:
1692                 this_->navigation=attr->u.navigation;
1693                 break;
1694         case attr_recent_dest:
1695                 this_->recentdest_count = attr->u.num;
1696                 break;
1697         case attr_speech:
1698                 this_->speech=attr->u.speech;
1699                 break;
1700         case attr_tracking:
1701                 this_->tracking=attr->u.tracking;
1702                 break;
1703         case attr_vehicle:
1704                 ret=navit_add_vehicle(this_, attr->u.vehicle);
1705                 break;
1706         case attr_timeout:
1707                 this_->center_timeout = attr->u.num;
1708                 break;
1709         case attr_autozoom:
1710                 this_->autozoom_secs = attr->u.num;
1711                 break;
1712         default:
1713                 return 0;
1714         }
1715         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1716         return ret;
1717 }
1718
1719 struct attr_iter *
1720 navit_attr_iter_new()
1721 {
1722         return g_new0(struct attr_iter, 1);
1723 }
1724
1725 void
1726 navit_attr_iter_destroy(struct attr_iter *iter)
1727 {
1728         g_free(iter);
1729 }
1730
1731 void
1732 navit_add_callback(struct navit *this_, struct callback *cb)
1733 {
1734         callback_list_add(this_->attr_cbl, cb);
1735 }
1736
1737 void
1738 navit_remove_callback(struct navit *this_, struct callback *cb)
1739 {
1740         callback_list_remove(this_->attr_cbl, cb);
1741 }
1742
1743 /**
1744  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
1745  *
1746  * @param navit The navit instance
1747  * @returns nothing
1748  */
1749
1750 static void
1751 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
1752 {
1753         struct point cursor_pnt;
1754         enum projection pro;
1755         struct attr cursor;
1756
1757         if (this_->blocked)
1758                 return;
1759         if (! vehicle_get_attr(nv->vehicle, attr_cursor, &cursor, NULL))
1760                 return;
1761         if (! cursor.u.cursor)
1762                 return;
1763         if (pnt)
1764                 cursor_pnt=*pnt;
1765         else {
1766                 pro=transform_get_projection(this_->trans);
1767                 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1768         }
1769         cursor_draw(cursor.u.cursor, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
1770 #if 0   
1771         if (pnt)
1772                 pnt2=*pnt;
1773         else {
1774                 pro=transform_get_projection(this_->trans);
1775                 transform(this_->trans, pro, &nv->coord, &pnt2, 1, 0);
1776         }
1777 #if 1
1778         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
1779 #else
1780         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
1781 #endif
1782 #endif
1783 }
1784
1785 static void
1786 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
1787 {
1788         struct attr attr_dir, attr_speed, attr_pos;
1789         struct pcoord cursor_pc;
1790         struct point cursor_pnt, *pnt=&cursor_pnt;
1791         enum projection pro;
1792         int border=16;
1793         int route_path_set=0;
1794         int recenter = 1; // indicates if we should recenter the map
1795
1796         profile(0,NULL);
1797         if (this_->ready != 3) {
1798                 profile(0,"return 1\n");
1799                 return;
1800         }
1801
1802         if (! vehicle_get_attr(nv->vehicle, attr_position_direction, &attr_dir, NULL) ||
1803             ! vehicle_get_attr(nv->vehicle, attr_position_speed, &attr_speed, NULL) ||
1804             ! vehicle_get_attr(nv->vehicle, attr_position_coord_geo, &attr_pos, NULL)) {
1805                 profile(0,"return 2\n");
1806                 return;
1807         }
1808         nv->dir=*attr_dir.u.numd;
1809         nv->speed=*attr_speed.u.numd;
1810         pro=transform_get_projection(this_->trans);
1811         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
1812         if (nv != this_->vehicle) {
1813                 navit_vehicle_draw(this_, nv, NULL);
1814                 profile(0,"return 3\n");
1815                 return;
1816         }
1817
1818         if (nv->speed < 10) {
1819                 long long diff,diff_x,diff_y;
1820
1821                 diff_x = abs(nv->coord.x - nv->last.x);
1822                 diff_y = abs(nv->coord.y - nv->last.y);
1823                 diff = (diff_x * diff_x) + (diff_y * diff_y);
1824
1825                 if ((diff < 20) && (diff > 0)) { // if our long is only 32 bit wide, we could run into an overflow here
1826                         recenter = 0;
1827                 }
1828         }
1829         if (recenter) {
1830                 nv->last = nv->coord;
1831         }
1832
1833         if (this_->route)
1834                 route_path_set=route_get_path_set(this_->route);
1835         cursor_pc.x = nv->coord.x;
1836         cursor_pc.y = nv->coord.y;
1837         cursor_pc.pro = pro;
1838         if (this_->tracking && this_->tracking_flag) {
1839                 if (tracking_update(this_->tracking, &cursor_pc, nv->dir)) {
1840                         nv->coord.x=cursor_pc.x;
1841                         nv->coord.y=cursor_pc.y;
1842                 }
1843         }
1844         if (this_->route) {
1845                 if (this_->tracking && this_->tracking_flag)
1846                         route_set_position_from_tracking(this_->route, this_->tracking);
1847                 else
1848                         route_set_position(this_->route, &cursor_pc);
1849         }
1850         callback_list_call_attr_0(this_->attr_cbl, attr_position);
1851         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
1852         transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1853         if (!transform_within_border(this_->trans, &cursor_pnt, border)) {
1854                 if (!this_->cursor_flag) {
1855                         profile(0,"return 4\n");
1856                         return;
1857                 }
1858                 if ((nv->follow_curr != 1) && ((time(NULL) - this_->last_moved) > this_->center_timeout) && (this_->button_pressed != 1) && (recenter)) {
1859                         if (this_->orientation != -1) {
1860                                 int dir=nv->dir-this_->orientation;
1861                                 navit_set_center_cursor(this_, &nv->coord, 0, 50 - 30.*sin(M_PI*dir/180.), 50 + 30.*cos(M_PI*dir/180.));
1862                         }
1863                         else
1864                                 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1865                         pnt=NULL;
1866                 }
1867         }
1868
1869 #ifndef _WIN32
1870         if (this_->pid && nv->speed > 2)
1871                 kill(this_->pid, SIGWINCH);
1872 #endif
1873         if ((nv->follow_curr == 1) && (!this_->button_pressed)) {
1874                 if (this_->cursor_flag && ((time(NULL) - this_->last_moved) > this_->center_timeout) && (recenter)) {
1875                         navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1876                         navit_autozoom(this_, &nv->coord, nv->speed);
1877                         pnt=NULL;
1878                 } else { // We don't want to center, but redraw because otherwise the old route "lags"
1879                         navit_draw(this_);
1880                 }
1881         }
1882         if (pnt && this_->route && !route_path_set && route_get_path_set(this_->route))
1883                 navit_draw(this_);
1884         if (nv->follow_curr > 1)
1885                 nv->follow_curr--;
1886         else
1887                 nv->follow_curr=nv->follow;
1888         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
1889         if (pnt)
1890                 navit_vehicle_draw(this_, nv, pnt);
1891
1892         /* Finally, if we reached our destination, stop navigation. */
1893         if (this_->route && route_destination_reached(this_->route)) {
1894                 navit_set_destination(this_, NULL, NULL);
1895         }
1896         profile(0,"return 5\n");
1897 }
1898
1899 /**
1900  * Set the position of the vehicle
1901  *
1902  * @param navit The navit instance
1903  * @param c The coordinate to set as position
1904  * @returns nothing
1905  */
1906
1907 void
1908 navit_set_position(struct navit *this_, struct pcoord *c)
1909 {
1910         if (this_->route) {
1911                 route_set_position(this_->route, c);
1912                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1913         }
1914         if (this_->ready == 3)
1915                 navit_draw(this_);
1916 }
1917
1918 static void
1919 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
1920 {
1921         this_->vehicle=nv;
1922 }
1923
1924 /**
1925  * Register a new vehicle
1926  *
1927  * @param navit The navit instance
1928  * @param v The vehicle instance
1929  * @returns 1 for success
1930  */
1931 static int
1932 navit_add_vehicle(struct navit *this_, struct vehicle *v)
1933 {
1934         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
1935         struct attr follow,color,active, color2, animate;
1936         nv->vehicle=v;
1937         nv->follow=0;
1938         nv->last.x = 0;
1939         nv->last.y = 0;
1940         nv->animate_cursor=0;
1941         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
1942                 nv->follow=nv->follow=follow.u.num;
1943         if ((vehicle_get_attr(v, attr_color, &color, NULL)))
1944                 nv->c=*(color.u.color);
1945         if ((vehicle_get_attr(v, attr_color2, &color2, NULL)))
1946                 nv->c2=color2.u.color;
1947         else
1948                 nv->c2=NULL;
1949         nv->follow_curr=nv->follow;
1950         this_->vehicles=g_list_append(this_->vehicles, nv);
1951         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
1952                 navit_set_vehicle(this_, nv);
1953         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
1954                 nv->animate_cursor=animate.u.num;
1955         nv->callback.type=attr_callback;
1956         nv->callback.u.callback=callback_new_2(callback_cast(navit_vehicle_update), this_, nv);
1957         vehicle_add_attr(nv->vehicle, &nv->callback);
1958         vehicle_set_attr(nv->vehicle, &this_->self, NULL);
1959         return 1;
1960 }
1961
1962
1963
1964
1965 struct gui *
1966 navit_get_gui(struct navit *this_)
1967 {
1968         return this_->gui;
1969 }
1970
1971 struct transformation *
1972 navit_get_trans(struct navit *this_)
1973 {
1974         return this_->trans;
1975 }
1976
1977 struct route *
1978 navit_get_route(struct navit *this_)
1979 {
1980         return this_->route;
1981 }
1982
1983 struct navigation *
1984 navit_get_navigation(struct navit *this_)
1985 {
1986         return this_->navigation;
1987 }
1988
1989 struct displaylist *
1990 navit_get_displaylist(struct navit *this_)
1991 {
1992         return this_->displaylist;
1993 }
1994
1995 int
1996 navit_block(struct navit *this_, int block)
1997 {
1998         if (block) {
1999                 this_->blocked |= 1;
2000                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2001                         this_->blocked |= 2;
2002                 return 0;
2003         }
2004         if (this_->blocked & 2) {
2005                 this_->blocked=0;
2006                 navit_draw(this_);
2007                 return 1;
2008         }
2009         this_->blocked=0;
2010         return 0;
2011 }
2012
2013 void
2014 navit_destroy(struct navit *this_)
2015 {
2016         /* TODO: destroy objects contained in this_ */
2017         if (this_->vehicle)
2018                 vehicle_destroy(this_->vehicle->vehicle);
2019         main_remove_navit(this_);
2020         char *center_file = navit_get_center_file(TRUE);
2021         navit_write_center_to_file(this_, center_file);
2022         g_free(center_file);
2023         callback_destroy(this_->nav_speech_cb);
2024         callback_destroy(this_->roadbook_callback);
2025         callback_destroy(this_->popup_callback);
2026         callback_destroy(this_->motion_timeout_callback);
2027         graphics_remove_callback(this_->gra, this_->resize_callback);
2028         callback_destroy(this_->resize_callback);
2029         graphics_remove_callback(this_->gra, this_->button_callback);
2030         callback_destroy(this_->button_callback);
2031         graphics_remove_callback(this_->gra, this_->motion_callback);
2032         callback_destroy(this_->motion_callback);
2033         g_free(this_);
2034 }
2035
2036 /** @} */