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