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