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