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