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