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