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