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