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