Fix:Core:Moved callback to better position for initial osd drawing
[profile/ivi/navit.git] / navit / navit / navit.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2009 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 "popup.h"
46 #include "data_window.h"
47 #include "route.h"
48 #include "navigation.h"
49 #include "speech.h"
50 #include "track.h"
51 #include "vehicle.h"
52 #include "layout.h"
53 #include "log.h"
54 #include "attr.h"
55 #include "event.h"
56 #include "file.h"
57 #include "profile.h"
58 #include "command.h"
59 #include "navit_nls.h"
60 #include "util.h"
61 #include "messages.h"
62 #include "vehicleprofile.h"
63 #include "sunriset.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 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 tracking_flag;
103         int orientation;
104         int recentdest_count;
105         int osd_configuration;
106         GList *vehicles;
107         GList *windows_items;
108         struct navit_vehicle *vehicle;
109         struct callback_list *attr_cbl;
110         struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb;
111         struct datawindow *roadbook_window;
112         struct map *bookmark;
113         struct map *former_destination;
114         GHashTable *bookmarks_hash;
115         struct point pressed, last, current;
116         int button_pressed,moved,popped,zoomed;
117         int center_timeout;
118         int autozoom_secs;
119         int autozoom_min;
120         int autozoom_active;
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         struct messagelist *messages;
133         struct callback *resize_callback,*button_callback,*motion_callback;
134         struct vehicleprofile *vehicleprofile;
135         GList *vehicleprofiles;
136         int pitch;
137         int follow_cursor;
138         int prevTs;
139         int graphics_flags;
140         int zoom_min, zoom_max;
141 };
142
143 struct gui *main_loop_gui;
144
145 struct attr_iter {
146         union {
147                 GList *list;
148                 struct mapset_handle *mapset_handle;
149         } u;
150 };
151
152 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
153 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
154 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
155 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
156 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir);
157 static void navit_set_cursors(struct navit *this_);
158 static void navit_cmd_zoom_to_route(struct navit *this);
159 static void navit_cmd_set_center_cursor(struct navit *this_);
160 static void navit_cmd_announcer_toggle(struct navit *this_);
161 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
162
163 void
164 navit_add_mapset(struct navit *this_, struct mapset *ms)
165 {
166         this_->mapsets = g_list_append(this_->mapsets, ms);
167 }
168
169 struct mapset *
170 navit_get_mapset(struct navit *this_)
171 {
172         if(this_->mapsets){
173                 return this_->mapsets->data;
174         } else {
175                 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
176         }
177         return NULL;
178 }
179
180 struct tracking *
181 navit_get_tracking(struct navit *this_)
182 {
183         return this_->tracking;
184 }
185
186 static void
187 navit_draw_async(struct navit *this_, int async)
188 {
189         GList *l;
190         struct navit_vehicle *nv;
191
192         if (this_->blocked) {
193                 this_->blocked |= 2;
194                 return;
195         }
196         transform_setup_source_rect(this_->trans);
197         l=this_->vehicles;
198         while (l) {
199                 nv=l->data;
200                 navit_vehicle_draw(this_, nv, NULL);
201                 l=g_list_next(l);
202         }
203         graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
204 }
205
206 void
207 navit_draw(struct navit *this_)
208 {
209         navit_draw_async(this_, 0);
210 }
211
212 int
213 navit_get_ready(struct navit *this_)
214 {
215         return this_->ready;
216 }
217
218
219
220 void
221 navit_draw_displaylist(struct navit *this_)
222 {
223         if (this_->ready == 3)
224                 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
225 }
226
227 static void
228 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
229 {
230         int updated;
231         if (attr->type != attr_route_status)
232                 return;
233         updated=attr->u.num;
234         if (this_->ready != 3)
235                 return;
236         if (updated != route_status_path_done_new)
237                 return;
238         if (this_->vehicle) {
239                 if (this_->vehicle->follow_curr == 1)
240                         return;
241                 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
242                         this_->vehicle->follow_curr=this_->vehicle->follow;
243         }
244         navit_draw(this_);
245 }
246
247 void
248 navit_handle_resize(struct navit *this_, int w, int h)
249 {
250         struct map_selection sel;
251         int callback=(this_->ready == 1);
252         this_->ready |= 2;
253         if (this_->w != w || this_->h != h) {
254                 memset(&sel, 0, sizeof(sel));
255                 this_->w=w;
256                 this_->h=h;
257                 sel.u.p_rect.rl.x=w;
258                 sel.u.p_rect.rl.y=h;
259                 transform_set_screen_selection(this_->trans, &sel);
260                 graphics_init(this_->gra);
261                 graphics_set_rect(this_->gra, &sel.u.p_rect);
262         }
263         if (callback) 
264                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
265         if (this_->ready == 3)
266                 navit_draw(this_);
267 }
268
269 static void
270 navit_resize(void *data, int w, int h)
271 {
272         struct navit *this=data;
273         if (!this->ignore_graphics_events)
274                 navit_handle_resize(this, w, h);
275 }
276
277 int
278 navit_get_width(struct navit *this_)
279 {
280         return this_->w;
281 }
282
283
284 int
285 navit_get_height(struct navit *this_)
286 {
287         return this_->h;
288 }
289
290 static void
291 navit_popup(void *data)
292 {
293         struct navit *this_=data;
294         popup(this_, 1, &this_->pressed);
295         this_->button_timeout=NULL;
296         this_->popped=1;
297 }
298
299
300 int
301 navit_ignore_button(struct navit *this_)
302 {
303         if (this_->ignore_button)
304                 return 1;
305         this_->ignore_button=1;
306         return 0;
307 }
308
309 void
310 navit_ignore_graphics_events(struct navit *this_, int ignore)
311 {
312         this_->ignore_graphics_events=ignore;
313 }
314
315 static void
316 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
317 {
318         struct coord co,cn;
319         struct coord c,*cp;
320         int yaw;
321         double angleo,anglen;
322
323         if (!transform_reverse(tr, old, &co))
324                 return;
325         if (rot) {
326                 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
327                 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
328                 yaw=transform_get_yaw(tr)+angleo-anglen;
329                 transform_set_yaw(tr, yaw % 360);
330         }
331         if (!transform_reverse(tr, new, &cn))
332                 return;
333         cp=transform_get_center(tr);
334         c.x=cp->x+co.x-cn.x;
335         c.y=cp->y+co.y-cn.y;
336         dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
337         transform_set_center(tr, &c);
338 }
339
340 static void
341 navit_set_timeout(struct navit *this_)
342 {
343         struct attr follow;
344         follow.type=attr_follow;
345         follow.u.num=this_->center_timeout;
346         navit_set_attr(this_, &follow);
347 }
348
349 int
350 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
351 {
352         int border=16;
353
354         dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
355         callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
356         if (this_->ignore_button) {
357                 this_->ignore_button=0;
358                 return 0;
359         }
360         if (pressed) {
361                 this_->pressed=*p;
362                 this_->last=*p;
363                 this_->zoomed=0;
364                 if (button == 1) {
365                         this_->button_pressed=1;
366                         this_->moved=0;
367                         this_->popped=0;
368                         if (popup_callback)
369                                 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
370                 }
371                 if (button == 2)
372                         navit_set_center_screen(this_, p, 1);
373                 if (button == 3)
374                         popup(this_, button, p);
375                 if (button == 4 && this_->use_mousewheel) {
376                         this_->zoomed = 1;
377                         navit_zoom_in(this_, 2, p);
378                 }
379                 if (button == 5 && this_->use_mousewheel) {
380                         this_->zoomed = 1;
381                         navit_zoom_out(this_, 2, p);
382                 }
383         } else {
384
385                 this_->button_pressed=0;
386                 if (this_->button_timeout) {
387                         event_remove_timeout(this_->button_timeout);
388                         this_->button_timeout=NULL;
389                         if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
390                                 navit_set_center_screen(this_, p, !this_->zoomed);
391                         }
392                 }
393                 if (this_->motion_timeout) {
394                         event_remove_timeout(this_->motion_timeout);
395                         this_->motion_timeout=NULL;
396                 }
397                 if (this_->moved) {
398                         struct point pr;
399                         pr.x=this_->w/2;
400                         pr.y=this_->h;
401 #if 0
402                         update_transformation(this_->trans, &this_->pressed, p, &pr);
403 #else
404                         update_transformation(this_->trans, &this_->pressed, p, NULL);
405 #endif
406                         graphics_draw_drag(this_->gra, NULL);
407                         graphics_overlay_disable(this_->gra, 0);
408                         if (!this_->zoomed) 
409                                 navit_set_timeout(this_);
410                         navit_draw(this_);
411                 } else
412                         return 1;
413         }
414         return 0;
415 }
416
417 static void
418 navit_button(void *data, int pressed, int button, struct point *p)
419 {
420         struct navit *this=data;
421         dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
422         if (!this->ignore_graphics_events) {
423                 if (! this->popup_callback)
424                         this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
425                 navit_handle_button(this, pressed, button, p, this->popup_callback);
426         }
427 }
428
429
430 static void
431 navit_motion_timeout(struct navit *this_)
432 {
433         int dx, dy;
434
435         if (this_->drag_bitmap) {
436                 struct point point;
437                 point.x=(this_->current.x-this_->pressed.x);
438                 point.y=(this_->current.y-this_->pressed.y);
439                 if (graphics_draw_drag(this_->gra, &point)) {
440                         graphics_overlay_disable(this_->gra, 1);
441                         graphics_draw_mode(this_->gra, draw_mode_end);
442                         this_->moved=1;
443                         this_->motion_timeout=NULL;
444                         return;
445                 }
446         } 
447         dx=(this_->current.x-this_->last.x);
448         dy=(this_->current.y-this_->last.y);
449         if (dx || dy) {
450                 struct transformation *tr;
451                 struct point pr;
452                 this_->last=this_->current;
453                 graphics_overlay_disable(this_->gra, 1);
454                 tr=transform_dup(this_->trans);
455                 pr.x=this_->w/2;
456                 pr.y=this_->h;
457 #if 0
458                 update_transformation(tr, &this_->pressed, &this_->current, &pr);
459 #else
460                 update_transformation(tr, &this_->pressed, &this_->current, NULL);
461 #endif
462 #if 0
463                 graphics_displaylist_move(this_->displaylist, dx, dy);
464 #endif
465                 graphics_draw_cancel(this_->gra, this_->displaylist);
466                 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
467                 transform_destroy(tr);
468                 this_->moved=1;
469         }
470         this_->motion_timeout=NULL;
471         return;
472 }
473
474 void
475 navit_handle_motion(struct navit *this_, struct point *p)
476 {
477         int dx, dy;
478
479         if (this_->button_pressed && !this_->popped) {
480                 dx=(p->x-this_->pressed.x);
481                 dy=(p->y-this_->pressed.y);
482                 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
483                         this_->moved=1;
484                         if (this_->button_timeout) {
485                                 event_remove_timeout(this_->button_timeout);
486                                 this_->button_timeout=NULL;
487                         }
488                         this_->current=*p;
489                         if (! this_->motion_timeout_callback)
490                                 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
491                         if (! this_->motion_timeout)
492                                 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
493                 }
494         }
495 }
496
497 static void
498 navit_motion(void *data, struct point *p)
499 {
500         struct navit *this=data;
501         if (!this->ignore_graphics_events) 
502                 navit_handle_motion(this, p);
503 }
504
505 static void
506 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
507 {
508         struct coord c1, c2, *center;
509         if (scale < this_->zoom_min)
510                 scale=this_->zoom_min;
511         if (scale > this_->zoom_max)
512                 scale=this_->zoom_max;
513         if (p)
514                 transform_reverse(this_->trans, p, &c1);
515         transform_set_scale(this_->trans, scale);
516         if (p) {
517                 transform_reverse(this_->trans, p, &c2);
518                 center = transform_center(this_->trans);
519                 center->x += c1.x - c2.x;
520                 center->y += c1.y - c2.y;
521         }
522         if (draw)
523                 navit_draw(this_);
524 }
525
526 /**
527  * @brief Automatically adjusts zoom level
528  *
529  * This function automatically adjusts the current
530  * zoom level according to the current speed.
531  *
532  * @param this_ The navit struct
533  * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
534  * @param speed The vehicles speed in meters per second
535  * @param dir The direction into which the vehicle moves
536  */
537 static void
538 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
539 {
540         struct point pc;
541         int distance,w,h;
542         double new_scale;
543         long scale;
544
545         if (! this_->autozoom_active) {
546                 return;
547         }
548
549         distance = speed * this_->autozoom_secs;
550
551         transform_get_size(this_->trans, &w, &h);
552         transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
553         scale = transform_get_scale(this_->trans);
554
555         /* We make sure that the point we want to see is within a certain range
556          * around the vehicle. The radius of this circle is the size of the
557          * screen. This doesn't necessarily mean the point is visible because of
558          * perspective etc. Quite rough, but should be enough. */
559         
560         if (w > h) {
561                 new_scale = (double)distance / h * 16; 
562         } else {
563                 new_scale = (double)distance / w * 16; 
564         }
565
566         if (abs(new_scale - scale) < 2) { 
567                 return; // Smoothing
568         }
569         
570         if (new_scale >= this_->autozoom_min) {
571                 navit_scale(this_, (long)new_scale, &pc, 0);
572         } else {
573                 if (scale != this_->autozoom_min) {
574                         navit_scale(this_, this_->autozoom_min, &pc, 0);
575                 }
576         }
577 }
578
579 /**
580  * Change the current zoom level, zooming closer to the ground
581  *
582  * @param navit The navit instance
583  * @param factor The zoom factor, usually 2
584  * @param p The invariant point (if set to NULL, default to center)
585  * @returns nothing
586  */
587 void
588 navit_zoom_in(struct navit *this_, int factor, struct point *p)
589 {
590         long scale=transform_get_scale(this_->trans)/factor;
591         if (scale < 1)
592                 scale=1;
593         navit_scale(this_, scale, p, 1);
594 }
595
596 /**
597  * Change the current zoom level
598  *
599  * @param navit The navit instance
600  * @param factor The zoom factor, usually 2
601  * @param p The invariant point (if set to NULL, default to center)
602  * @returns nothing
603  */
604 void
605 navit_zoom_out(struct navit *this_, int factor, struct point *p)
606 {
607         long scale=transform_get_scale(this_->trans)*factor;
608         navit_scale(this_, scale, p, 1);
609 }
610
611 static int
612 navit_cmd_zoom_in(struct navit *this_)
613 {
614         struct point p;
615         if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
616                 navit_zoom_in(this_, 2, &p);
617                 this_->vehicle->follow_curr=this_->vehicle->follow;
618         } else
619                 navit_zoom_in(this_, 2, NULL);
620         return 0;
621 }
622
623 static int
624 navit_cmd_zoom_out(struct navit *this_)
625 {
626         struct point p;
627         if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
628                 navit_zoom_out(this_, 2, &p);
629                 this_->vehicle->follow_curr=this_->vehicle->follow;
630         } else
631                 navit_zoom_out(this_, 2, NULL);
632         return 0;
633 }
634
635
636 static void
637 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
638 {
639         if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) 
640                 navit_say(this, in[0]->u.str);
641 }
642
643 static void
644 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
645 {
646         struct pcoord pc;
647         char *description=NULL;
648         if (!in)
649                 return;
650         if (!in[0])
651                 return;
652         pc.pro = transform_get_projection(this->trans);
653         if (ATTR_IS_COORD(in[0]->type)) {
654                 pc.x=in[0]->u.coord->x;
655                 pc.y=in[0]->u.coord->y;
656                 in++;
657         } else if (ATTR_IS_PCOORD(in[0]->type)) {
658                 pc=*in[0]->u.pcoord;
659                 in++;
660         } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
661                 pc.pro=in[0]->u.num;
662                 pc.x=in[1]->u.num;
663                 pc.y=in[2]->u.num;
664                 in+=3;
665         } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
666                 pc.x=in[0]->u.num;
667                 pc.y=in[1]->u.num;
668                 in+=2;
669         } else
670                 return;
671         if (in[0] && ATTR_IS_STRING(in[0]->type))
672                 description=in[0]->u.str;
673         navit_set_destination(this, &pc, description, 1);
674 }
675
676 static struct command_table commands[] = {
677         {"zoom_in",command_cast(navit_cmd_zoom_in)},
678         {"zoom_out",command_cast(navit_cmd_zoom_out)},
679         {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
680         {"say",command_cast(navit_cmd_say)},
681         {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
682         {"set_destination",command_cast(navit_cmd_set_destination)},
683         {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
684 };
685         
686
687 struct navit *
688 navit_new(struct attr *parent, struct attr **attrs)
689 {
690         struct navit *this_=g_new0(struct navit, 1);
691         struct pcoord center;
692         struct coord co;
693         struct coord_geo g;
694         enum projection pro=projection_mg;
695         int zoom = 256;
696         g.lat=53.13;
697         g.lng=11.70;
698
699         this_->self.type=attr_navit;
700         this_->self.u.navit=this_;
701         this_->attr_cbl=callback_list_new();
702
703         this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
704
705         this_->orientation=-1;
706         this_->tracking_flag=1;
707         this_->recentdest_count=10;
708         this_->osd_configuration=-1;
709
710         this_->center_timeout = 10;
711         this_->use_mousewheel = 1;
712         this_->autozoom_secs = 10;
713         this_->autozoom_min = 7;
714         this_->autozoom_active = 0;
715         this_->zoom_min = 1;
716         this_->zoom_max = 2097152;
717         this_->follow_cursor = 1;
718
719         this_->trans = transform_new();
720         transform_from_geo(pro, &g, &co);
721         center.x=co.x;
722         center.y=co.y;
723         center.pro = pro;
724         
725         this_->prevTs=0;
726
727         transform_setup(this_->trans, &center, zoom, (this_->orientation != -1) ? this_->orientation : 0);
728         for (;*attrs; attrs++) {
729                 navit_set_attr_do(this_, *attrs, 1);
730         }
731         this_->displaylist=graphics_displaylist_new();
732         command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
733
734         this_->messages = messagelist_new(attrs);
735         
736         return this_;
737 }
738
739 static int
740 navit_set_gui(struct navit *this_, struct gui *gui)
741 {
742         if (this_->gui)
743                 return 0;
744         this_->gui=gui;
745         if (gui_has_main_loop(this_->gui)) {
746                 if (! main_loop_gui) {
747                         main_loop_gui=this_->gui;
748                 } else {
749                         dbg(0,"gui with main loop already active, ignoring this instance");
750                         return 0;
751                 }
752         }
753         return 1;
754 }
755
756 void 
757 navit_add_message(struct navit *this_, char *message)
758 {
759         message_new(this_->messages, message);
760 }
761
762 struct message
763 *navit_get_messages(struct navit *this_)
764 {
765         return message_get(this_->messages);
766 }
767
768 static int
769 navit_set_graphics(struct navit *this_, struct graphics *gra)
770 {
771         if (this_->gra)
772                 return 0;
773         this_->gra=gra;
774         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
775         graphics_add_callback(gra, this_->resize_callback);
776         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
777         graphics_add_callback(gra, this_->button_callback);
778         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
779         graphics_add_callback(gra, this_->motion_callback);
780         return 1;
781 }
782
783 struct graphics *
784 navit_get_graphics(struct navit *this_)
785 {
786         return this_->gra;
787 }
788
789 struct vehicleprofile *
790 navit_get_vehicleprofile(struct navit *this_)
791 {
792         return this_->vehicleprofile;
793 }
794
795 GList *
796 navit_get_vehicleprofiles(struct navit *this_)
797 {
798         return this_->vehicleprofiles;
799 }
800
801 static void
802 navit_projection_set(struct navit *this_, enum projection pro)
803 {
804         struct coord_geo g;
805         struct coord *c;
806
807         c=transform_center(this_->trans);
808         transform_to_geo(transform_get_projection(this_->trans), c, &g);
809         transform_set_projection(this_->trans, pro);
810         transform_from_geo(pro, &g, c);
811         navit_draw(this_);
812 }
813
814 /**
815  * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
816  */
817 static void
818 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
819 {
820         FILE *f;
821         int offset=0;
822         char *buffer;
823         int ch,prev,lines=0;
824         int numc,readc;
825         int fd;
826         const char *prostr;
827
828         f=fopen(file, "r");
829         if (!f)
830                 goto new_file;
831         if (limit != 0) {
832                 prev = '\n';
833                 while ((ch = fgetc(f)) != EOF) {
834                         if ((ch == '\n') && (prev != '\n')) {
835                                 lines++;
836                         }
837                         prev = ch;
838                 }
839
840                 if (prev != '\n') { // Last line did not end with a newline
841                         lines++;
842                 }
843
844                 fclose(f);
845                 f = fopen(file, "r+");
846                 fd = fileno(f);
847                 while (lines >= limit) { // We have to "scroll up"
848                         rewind(f);
849                         numc = 0; // Counts how many bytes we have in our line to scroll up
850                         while ((ch = fgetc(f)) != EOF) {
851                                 numc++;
852                                 if (ch == '\n') {
853                                         break;
854                                 }
855                         }
856
857                         buffer=g_malloc(numc);
858                         offset = numc; // Offset holds where we currently are
859                         
860                         do {
861                                 fseek(f,offset,SEEK_SET);
862                                 readc = fread(buffer,1,numc,f);
863                                 
864                                 fseek(f,-(numc+readc),SEEK_CUR);
865                                 fwrite(buffer,1,readc,f);
866
867                                 offset += readc;
868                         } while (readc == numc);
869
870                         g_free(buffer);
871                         fflush(f);
872                         ftruncate(fd,(offset-numc));
873 #ifdef HAVE_FSYNC
874                         fsync(fd);
875 #endif
876
877                         lines--;
878                 }
879                 fclose(f);
880         }
881
882 new_file:
883         f=fopen(file, "a");
884         if (f) {
885                 if (c) {
886                         prostr = projection_to_name(c->pro,NULL);
887                         fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
888                                  prostr, *prostr ? ":" : "", 
889                                  c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x, 
890                                  c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y, 
891                                  type, description);
892                 } else
893                         fprintf(f,"\n");
894         }
895         fclose(f);
896 }
897
898 /*
899  * navit_get_user_data_directory
900  * 
901  * returns the directory used to store user data files (center.txt,
902  * destination.txt, bookmark.txt, ...)
903  *
904  * arg: gboolean create: create the directory if it does not exist
905  */
906 static char*
907 navit_get_user_data_directory(gboolean create) {
908         char *dir;
909         dir = getenv("NAVIT_USER_DATADIR");
910         if (create && !file_exists(dir)) {
911                 dbg(0,"creating dir %s\n", dir);
912                 if (file_mkdir(dir,0)) {
913                         dbg(0,"failed creating dir %s\n", dir);
914                         return NULL;
915                 }
916         }
917
918         return dir;
919 }
920
921 /*
922  * navit_get_destination_file
923  * 
924  * returns the name of the file used to store destinations with its
925  * full path
926  *
927  * arg: gboolean create: create the directory where the file is stored
928  * if it does not exist
929  */
930 static char*
931 navit_get_destination_file(gboolean create)
932 {
933         return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
934 }
935
936 /*
937  * navit_get_bookmark_file
938  * 
939  * returns the name of the file used to store bookmarks with its
940  * full path
941  *
942  * arg: gboolean create: create the directory where the file is stored
943  * if it does not exist
944  */
945 static char*
946 navit_get_bookmark_file(gboolean create)
947 {
948         return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
949 }
950
951
952 /*
953  * navit_get_bookmark_file
954  * 
955  * returns the name of the file used to store the center file  with its
956  * full path
957  *
958  * arg: gboolean create: create the directory where the file is stored
959  * if it does not exist
960  */
961 static char*
962 navit_get_center_file(gboolean create)
963 {
964         return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
965 }
966
967 static void
968 navit_set_center_from_file(struct navit *this_, char *file)
969 {
970 #ifndef HAVE_API_ANDROID
971         FILE *f;
972         char *line = NULL;
973
974         size_t line_size = 0;
975         enum projection pro;
976         struct coord *center;
977
978         f = fopen(file, "r");
979         if (! f)
980                 return;
981         getline(&line, &line_size, f);
982         fclose(f);
983         if (line) {
984                 center = transform_center(this_->trans);
985                 pro = transform_get_projection(this_->trans);
986                 coord_parse(g_strchomp(line), pro, center);
987                 free(line);
988         }
989         return;
990 #endif
991 }
992  
993 static void
994 navit_write_center_to_file(struct navit *this_, char *file)
995 {
996         FILE *f;
997         enum projection pro;
998         struct coord *center;
999
1000         f = fopen(file, "w+");
1001         if (f) {
1002                 center = transform_center(this_->trans);
1003                 pro = transform_get_projection(this_->trans);
1004                 coord_print(pro, center, f);
1005                 fclose(f);
1006         } else {
1007                 perror(file);
1008         }
1009         return;
1010 }
1011
1012
1013 /**
1014  * Start the route computing to a given set of coordinates
1015  *
1016  * @param navit The navit instance
1017  * @param c The coordinate to start routing to
1018  * @param description A label which allows the user to later identify this destination in the former destinations selection
1019  * @returns nothing
1020  */
1021 void
1022 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1023 {
1024         if (c) {
1025                 this_->destination=*c;
1026                 this_->destination_valid=1;
1027         } else
1028                 this_->destination_valid=0;
1029         char *destination_file = navit_get_destination_file(TRUE);
1030         navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
1031         g_free(destination_file);
1032         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1033         if (this_->route) {
1034                 route_set_destination(this_->route, c, async);
1035
1036                 if (this_->ready == 3)
1037                         navit_draw(this_);
1038         }
1039 }
1040
1041 /**
1042  * @brief Checks if a route is calculated
1043  *
1044  * This function checks if a route is calculated.
1045  *
1046  * @param this_ The navit struct whose route should be checked.
1047  * @return True if the route is set, false otherwise.
1048  */
1049 int
1050 navit_check_route(struct navit *this_)
1051 {
1052         if (this_->route) {
1053                 return route_get_path_set(this_->route);
1054         }
1055
1056         return 0;
1057 }
1058
1059 /**
1060  * Record the given set of coordinates as a bookmark
1061  *
1062  * @param navit The navit instance
1063  * @param c The coordinate to store
1064  * @param description A label which allows the user to later identify this bookmark
1065  * @returns nothing
1066  */
1067 void
1068 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
1069 {
1070         char *bookmark_file = navit_get_bookmark_file(TRUE);
1071         navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
1072         g_free(bookmark_file);
1073
1074         callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
1075 }
1076
1077 struct navit *global_navit;
1078
1079 static void
1080 navit_add_bookmarks_from_file(struct navit *this_)
1081 {
1082         char *bookmark_file = navit_get_bookmark_file(FALSE);
1083         struct attr parent={attr_navit, .u.navit=this_};
1084         struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
1085         struct attr *attrs[]={&type, &data, NULL};
1086
1087         this_->bookmark=map_new(&parent, attrs);
1088         g_free(bookmark_file);
1089 }
1090
1091 static int
1092 navit_former_destinations_active(struct navit *this_)
1093 {
1094         char *destination_file = navit_get_destination_file(FALSE);
1095         FILE *f;
1096         int active=0;
1097         char buffer[3];
1098         f=fopen(destination_file,"r");
1099         if (f) {
1100                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
1101                         active=1;
1102                 fclose(f);
1103         }
1104         g_free(destination_file);
1105         return active;
1106 }
1107
1108 static void
1109 navit_add_former_destinations_from_file(struct navit *this_)
1110 {
1111         char *destination_file = navit_get_destination_file(FALSE);
1112         struct attr parent={attr_navit, .u.navit=this_};
1113         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
1114         struct attr *attrs[]={&type, &data, NULL};
1115         struct map_rect *mr;
1116         struct item *item;
1117         int valid=0;
1118         struct coord c;
1119         struct pcoord pc;
1120
1121         this_->former_destination=map_new(&parent, attrs);
1122         g_free(destination_file);
1123         if (!this_->route || !navit_former_destinations_active(this_))
1124                 return; 
1125         mr=map_rect_new(this_->former_destination, NULL);
1126         while ((item=map_rect_get_item(mr))) {
1127                 if (item->type == type_former_destination && item_coord_get(item, &c, 1)) 
1128                         valid=1;
1129         }
1130         map_rect_destroy(mr);
1131         pc.pro=map_projection(this_->former_destination);
1132         pc.x=c.x;
1133         pc.y=c.y;
1134         if (valid) {
1135                 route_set_destination(this_->route, &pc, 1);
1136                 this_->destination=pc;
1137                 this_->destination_valid=1;
1138         }
1139 }
1140
1141
1142 void
1143 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1144 {
1145         va_list ap;
1146         char *str1,*str2;
1147         va_start(ap, fmt);
1148         if (this_->textfile_debug_log && this_->vehicle) {
1149                 str1=g_strdup_vprintf(fmt, ap);
1150                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1151                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1152                 g_free(str2);
1153                 g_free(str1);
1154         }
1155         va_end(ap);
1156 }
1157
1158 int 
1159 navit_speech_estimate(struct navit *this_, char *str)
1160 {
1161         return speech_estimate_duration(this_->speech, str);
1162 }
1163
1164 void
1165 navit_say(struct navit *this_, char *text)
1166 {
1167         speech_say(this_->speech, text);
1168 }
1169
1170 /**
1171  * @brief Toggles the navigation announcer for navit
1172  * @param this_ The navit object
1173  */
1174 static void
1175 navit_cmd_announcer_toggle(struct navit *this_)
1176 {
1177     struct attr attr, speechattr;
1178
1179     // search for the speech attribute
1180     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1181         return;
1182     // find out if the corresponding attribute attr_active has been set
1183     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1184         // flip it then...
1185         attr.u.num = !attr.u.num;
1186     } else {
1187         // otherwise disable it because voice is enabled by default
1188         attr.type = attr_active;
1189         attr.u.num = 0;
1190     }
1191
1192     // apply the new state
1193     if(!speech_set_attr(speechattr.u.speech, &attr))
1194         return;
1195
1196     // announce that the speech attribute has changed
1197     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1198 }
1199
1200 void
1201 navit_speak(struct navit *this_)
1202 {
1203         struct navigation *nav=this_->navigation;
1204         struct map *map=NULL;
1205         struct map_rect *mr=NULL;
1206         struct item *item;
1207         struct attr attr;
1208
1209     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1210         attr.u.num = 1;
1211     dbg(1, "this_.speech->active %i\n", attr.u.num);
1212     if(!attr.u.num)
1213         return;
1214
1215         if (nav)
1216                 map=navigation_get_map(nav);
1217         if (map)
1218                 mr=map_rect_new(map, NULL);
1219         if (mr) {
1220                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1221                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1222                         speech_say(this_->speech, attr.u.str);
1223                         navit_add_message(this_, attr.u.str);
1224                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1225                 }
1226                 map_rect_destroy(mr);
1227         }
1228 }
1229
1230 static void
1231 navit_window_roadbook_update(struct navit *this_)
1232 {
1233         struct navigation *nav=this_->navigation;
1234         struct map *map=NULL;
1235         struct map_rect *mr=NULL;
1236         struct item *item;
1237         struct attr attr;
1238         struct param_list param[5];
1239         int secs;
1240
1241         dbg(1,"enter\n");
1242         datawindow_mode(this_->roadbook_window, 1);
1243         if (nav)
1244                 map=navigation_get_map(nav);
1245         if (map)
1246                 mr=map_rect_new(map, NULL);
1247         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1248         if (mr) {
1249                 dbg(0,"while loop\n");
1250                 while ((item=map_rect_get_item(mr))) {
1251                         dbg(0,"item=%p\n", item);
1252                         attr.u.str=NULL;
1253                         if (item->type != type_nav_position) {
1254                                 item_attr_get(item, attr_navigation_long, &attr);
1255                                 if (attr.u.str == NULL) {
1256                                         continue;
1257                                 }
1258                                 dbg(2, "Command='%s'\n", attr.u.str);
1259                                 param[0].value=g_strdup(attr.u.str);
1260                         } else
1261                                 param[0].value=_("Position");
1262                         param[0].name=_("Command");
1263
1264                         item_attr_get(item, attr_length, &attr);
1265                         dbg(2, "Length=%d\n", attr.u.num);
1266                         param[1].name=_("Length");
1267
1268                         if ( attr.u.num >= 2000 )
1269                         {
1270                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1271                         }
1272                         else
1273                         {
1274                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1275                         }
1276
1277                         item_attr_get(item, attr_time, &attr);
1278                         dbg(2, "Time=%d\n", attr.u.num);
1279                         secs=attr.u.num/10;
1280                         param[2].name=_("Time");
1281                         if ( secs >= 3600 )
1282                         {
1283                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1284                         }
1285                         else
1286                         {
1287                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1288                         }
1289
1290                         item_attr_get(item, attr_destination_length, &attr);
1291                         dbg(2, "Destlength=%d\n", attr.u.num);
1292                         param[3].name=_("Destination Length");
1293                         if ( attr.u.num >= 2000 )
1294                         {
1295                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1296                         }
1297                         else
1298                         {
1299                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1300                         }
1301
1302                         item_attr_get(item, attr_destination_time, &attr);
1303                         dbg(2, "Desttime=%d\n", attr.u.num);
1304                         secs=attr.u.num/10;
1305                         param[4].name=_("Destination Time");
1306                         if ( secs >= 3600 )
1307                         {
1308                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1309                         }
1310                         else
1311                         {
1312                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1313                         }
1314                         datawindow_add(this_->roadbook_window, param, 5);
1315                 }
1316                 map_rect_destroy(mr);
1317         }
1318         datawindow_mode(this_->roadbook_window, 0);
1319 }
1320
1321 void
1322 navit_window_roadbook_destroy(struct navit *this_)
1323 {
1324         dbg(0, "enter\n");
1325         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1326         this_->roadbook_window=NULL;
1327         this_->roadbook_callback=NULL;
1328 }
1329 void
1330 navit_window_roadbook_new(struct navit *this_)
1331 {
1332         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1333                 return;
1334         }
1335
1336         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1337         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1338         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1339         navit_window_roadbook_update(this_);
1340 }
1341
1342 void
1343 navit_init(struct navit *this_)
1344 {
1345         struct mapset *ms;
1346         struct map *map;
1347         int callback;
1348
1349         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1350         if (!this_->gui) {
1351                 dbg(0,"no gui\n");
1352                 navit_destroy(this_);
1353                 return;
1354         }
1355         if (!this_->gra) {
1356                 dbg(0,"no graphics\n");
1357                 navit_destroy(this_);
1358                 return;
1359         }
1360         dbg(2,"Connecting gui to graphics\n");
1361         if (gui_set_graphics(this_->gui, this_->gra)) {
1362                 struct attr attr_type_gui, attr_type_graphics;
1363                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1364                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1365                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1366                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1367                 dbg(0," for explanations and solutions\n");
1368
1369                 navit_destroy(this_);
1370                 return;
1371         }
1372         dbg(2,"Initializing graphics\n");
1373         dbg(2,"Setting Vehicle\n");
1374         navit_set_vehicle(this_, this_->vehicle);
1375         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1376         if (this_->mapsets) {
1377                 ms=this_->mapsets->data;
1378                 if (this_->route) {
1379                         if ((map=route_get_map(this_->route)))
1380                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1381                         if ((map=route_get_graph_map(this_->route))) {
1382                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1383                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1384                         }
1385                         route_set_mapset(this_->route, ms);
1386                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1387                 }
1388                 if (this_->tracking) {
1389                         tracking_set_mapset(this_->tracking, ms);
1390                         if (this_->route)
1391                                 tracking_set_route(this_->tracking, this_->route);
1392                 }
1393                 if (this_->navigation) {
1394                         if ((map=navigation_get_map(this_->navigation))) {
1395                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1396                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1397                         }
1398                 }
1399                 if (this_->tracking) {
1400                         if ((map=tracking_get_map(this_->tracking))) {
1401                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1402                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1403                         }
1404                 }
1405                 navit_add_bookmarks_from_file(this_);
1406                 navit_add_former_destinations_from_file(this_);
1407         }
1408         if (this_->route) {
1409                 struct attr callback;
1410                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1411                 callback.type=attr_callback;
1412                 callback.u.callback=this_->route_cb;
1413                 route_add_attr(this_->route, &callback);
1414         }
1415         if (this_->navigation) {
1416                 if (this_->speech) {
1417                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1418                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1419                 }
1420                 if (this_->route)
1421                         navigation_set_route(this_->navigation, this_->route);
1422         }
1423         dbg(2,"Setting Center\n");
1424         char *center_file = navit_get_center_file(FALSE);
1425         navit_set_center_from_file(this_, center_file);
1426         g_free(center_file);
1427 #if 0
1428         if (this_->menubar) {
1429                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1430                 if (men) {
1431                         navit_add_menu_windows_items(this_, men);
1432                 }
1433         }
1434 #endif
1435         global_navit=this_;
1436 #if 0
1437         navit_window_roadbook_new(this_);
1438         navit_window_items_new(this_);
1439 #endif
1440
1441         messagelist_init(this_->messages);
1442
1443         navit_set_cursors(this_);
1444
1445         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1446         callback=(this_->ready == 2);
1447         this_->ready|=1;
1448         dbg(2,"ready=%d\n",this_->ready);
1449         if (this_->ready == 3)
1450                 navit_draw(this_);
1451         if (callback)
1452                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1453 #if 0
1454         routech_test(this_);
1455 #endif
1456 }
1457
1458 void
1459 navit_zoom_to_route(struct navit *this_, int orientation)
1460 {
1461         struct map *map;
1462         struct map_rect *mr=NULL;
1463         struct item *item;
1464         struct coord c;
1465         struct coord_rect r;
1466         int count=0,scale=16;
1467         if (! this_->route)
1468                 return;
1469         dbg(1,"enter\n");
1470         map=route_get_map(this_->route);
1471         dbg(1,"map=%p\n",map);
1472         if (map)
1473                 mr=map_rect_new(map, NULL);
1474         dbg(1,"mr=%p\n",mr);
1475         if (mr) {
1476                 while ((item=map_rect_get_item(mr))) {
1477                         dbg(1,"item=%s\n", item_to_name(item->type));
1478                         while (item_coord_get(item, &c, 1)) {
1479                                 dbg(1,"coord\n");
1480                                 if (!count) 
1481                                         r.lu=r.rl=c;
1482                                 else
1483                                         coord_rect_extend(&r, &c);      
1484                                 count++;
1485                         }
1486                 }
1487         }
1488         if (! count)
1489                 return;
1490         c.x=(r.rl.x+r.lu.x)/2;
1491         c.y=(r.rl.y+r.lu.y)/2;
1492         dbg(1,"count=%d\n",count);
1493         if (orientation != -1)
1494                 transform_set_yaw(this_->trans, orientation);
1495         transform_set_center(this_->trans, &c);
1496         dbg(1,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1497         while (scale < 1<<20) {
1498                 struct point p1,p2;
1499                 transform_set_scale(this_->trans, scale);
1500                 transform_setup_source_rect(this_->trans);
1501                 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1502                 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1503                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1504                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1505                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1506                         scale*=2;
1507                 else
1508                         break;
1509         
1510         }
1511         if (this_->ready == 3)
1512                 navit_draw_async(this_,0);
1513 }
1514
1515 static void
1516 navit_cmd_zoom_to_route(struct navit *this)
1517 {
1518         navit_zoom_to_route(this, 0);
1519 }
1520
1521
1522 /**
1523  * Change the current zoom level
1524  *
1525  * @param navit The navit instance
1526  * @param center The point where to center the map, including its projection
1527  * @returns nothing
1528  */
1529 void
1530 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1531 {
1532         struct coord *c=transform_center(this_->trans);
1533         struct coord c1,c2;
1534         enum projection pro = transform_get_projection(this_->trans);
1535         if (pro != center->pro) {
1536                 c1.x = center->x;
1537                 c1.y = center->y;
1538                 transform_from_to(&c1, center->pro, &c2, pro);
1539         } else {
1540                 c2.x = center->x;
1541                 c2.y = center->y;
1542         }
1543         *c=c2;
1544         if (set_timeout) 
1545                 navit_set_timeout(this_);
1546         if (this_->ready == 3)
1547                 navit_draw(this_);
1548 }
1549
1550 static void
1551 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1552 {
1553         int width, height;
1554         struct point po;
1555         transform_set_center(this_->trans, c);
1556         transform_get_size(this_->trans, &width, &height);
1557         po.x=width/2;
1558         po.y=height/2;
1559         update_transformation(this_->trans, &po, p, NULL);
1560         if (set_timeout)
1561                 navit_set_timeout(this_);
1562 }
1563
1564 /**
1565  * Links all vehicles to a cursor depending on the current profile.
1566  *
1567  * @param this_ A navit instance
1568  * @author Ralph Sennhauser (10/2009)
1569  */
1570 static void
1571 navit_set_cursors(struct navit *this_)
1572 {
1573         struct attr name;
1574         struct navit_vehicle *nv;
1575         struct cursor *c;
1576         GList *v;
1577
1578         v=g_list_first(this_->vehicles); // GList of navit_vehicles
1579         while (v) {
1580                 nv=v->data;
1581                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL))
1582                         c=layout_get_cursor(this_->layout_current, name.u.str);
1583                 else
1584                         c=layout_get_cursor(this_->layout_current, "default");
1585                 vehicle_set_cursor(nv->vehicle, c);
1586                 v=g_list_next(v);
1587         }
1588         return;
1589 }
1590
1591 static int
1592 navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir)
1593 {
1594         int width, height;
1595         struct navit_vehicle *nv=this_->vehicle;
1596
1597         float offset=30;            // Cursor offset from the center of the screen (percent).
1598 #if 0 /* Better improve track.c to get that issue resolved or make it configurable with being off the default, the jumping back to the center is a bit annoying */
1599         float min_offset = 0.;      // Percent offset at min_offset_speed.
1600         float max_offset = 30.;     // Percent offset at max_offset_speed.
1601         int min_offset_speed = 2;   // Speed in km/h
1602         int max_offset_speed = 50;  // Speed ini km/h
1603         // Calculate cursor offset from the center of the screen, upon speed.
1604         if (nv->speed <= min_offset_speed) {
1605             offset = min_offset;
1606         } else if (nv->speed > max_offset_speed) {
1607             offset = max_offset;
1608         } else {
1609             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1610         }
1611 #endif
1612
1613         transform_get_size(this_->trans, &width, &height);
1614         if (this_->orientation == -1) {
1615                 p->x=50*width/100;
1616                 p->y=(50 + offset)*height/100;
1617                 if (dir)
1618                         *dir=nv->dir;
1619         } else {
1620                 int mdir;
1621                 if (this_->tracking && this_->tracking_flag) {
1622                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1623                 } else {
1624                         mdir=nv->dir-this_->orientation;
1625                 }
1626
1627                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1628                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1629                 if (dir)
1630                         *dir=this_->orientation;
1631         }
1632         return 1;
1633 }
1634
1635 static void
1636 navit_set_center_cursor(struct navit *this_)
1637 {
1638         int dir;
1639         struct point pn;
1640         struct navit_vehicle *nv=this_->vehicle;
1641         navit_get_cursor_pnt(this_, &pn, &dir);
1642         transform_set_yaw(this_->trans, dir);
1643         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1644         navit_autozoom(this_, &nv->coord, nv->speed, 0);
1645         if (this_->ready == 3)
1646                 navit_draw_async(this_, 1);
1647 }
1648
1649 static void
1650 navit_cmd_set_center_cursor(struct navit *this_)
1651 {
1652         navit_set_center_cursor(this_);
1653 }
1654
1655 void
1656 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1657 {
1658         struct coord c;
1659         struct pcoord pc;
1660         transform_reverse(this_->trans, p, &c);
1661         pc.x = c.x;
1662         pc.y = c.y;
1663         pc.pro = transform_get_projection(this_->trans);
1664         navit_set_center(this_, &pc, set_timeout);
1665 }
1666
1667 #if 0
1668                 switch((*attrs)->type) {
1669                 case attr_zoom:
1670                         zoom=(*attrs)->u.num;
1671                         break;
1672                 case attr_center:
1673                         g=*((*attrs)->u.coord_geo);
1674                         break;
1675 #endif
1676
1677 static int
1678 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1679 {
1680         int dir=0, orient_old=0, attr_updated=0;
1681         struct coord co;
1682         long zoom;
1683         GList *l;
1684         struct navit_vehicle *nv;
1685         struct attr active=(struct attr){attr_active,{(void *)0}};
1686         struct layout *lay;
1687
1688         switch (attr->type) {
1689         case attr_autozoom:
1690                 attr_updated=(this_->autozoom_secs != attr->u.num);
1691                 this_->autozoom_secs = attr->u.num;
1692                 break;
1693         case attr_autozoom_active:
1694                 attr_updated=(this_->autozoom_active != attr->u.num);
1695                 this_->autozoom_active = attr->u.num;
1696                 break;
1697         case attr_center:
1698                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1699                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1700                 transform_set_center(this_->trans, &co);
1701                 break;
1702         case attr_drag_bitmap:
1703                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1704                 this_->drag_bitmap=!!attr->u.num;
1705                 break;
1706         case attr_flags_graphics:
1707                 attr_updated=(this_->graphics_flags != attr->u.num);
1708                 this_->graphics_flags=attr->u.num;
1709                 break;
1710         case attr_follow:
1711                 if (!this_->vehicle)
1712                         return 0;
1713                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1714                 this_->vehicle->follow_curr = attr->u.num;
1715                 break;
1716         case attr_layout:
1717                 if(this_->layout_current!=attr->u.layout) {
1718                         this_->layout_current=attr->u.layout;
1719                         graphics_font_destroy_all(this_->gra);
1720                         navit_set_cursors(this_);
1721                         if (this_->ready == 3)
1722                                 navit_draw(this_);
1723                         attr_updated=1;
1724                 }
1725                 break;
1726         case attr_layout_name:
1727                 l=this_->layouts;
1728                 while (l) {
1729                         lay=l->data;
1730                         if (!strcmp(lay->name,attr->u.str)) {
1731                                 struct attr attr;
1732                                 attr.type=attr_layout;
1733                                 attr.u.layout=lay;
1734                                 return navit_set_attr_do(this_, &attr, init);
1735                         }
1736                         l=g_list_next(l);
1737                 }               
1738                 return 0;
1739         case attr_orientation:
1740                 orient_old=this_->orientation;
1741                 this_->orientation=attr->u.num;
1742                 if (!init) {
1743                         if (this_->orientation != -1) {
1744                                 dir = this_->orientation;
1745                         } else {
1746                                 if (this_->vehicle) {
1747                                         dir = this_->vehicle->dir;
1748                                 }
1749                         }
1750                         transform_set_yaw(this_->trans, dir);
1751                         if (orient_old != this_->orientation) {
1752 #if 0
1753                                 if (this_->ready == 3)
1754                                         navit_draw(this_);
1755 #endif
1756                                 attr_updated=1;
1757                         }
1758                 }
1759                 break;
1760         case attr_osd_configuration:
1761                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1762                 attr_updated=(this_->osd_configuration != attr->u.num);
1763                 this_->osd_configuration=attr->u.num;
1764                 break;
1765         case attr_pitch:
1766                 attr_updated=(this_->pitch != attr->u.num);
1767                 this_->pitch=attr->u.num;
1768                 transform_set_pitch(this_->trans, this_->pitch);
1769                 if (!init && attr_updated && this_->ready == 3)
1770                         navit_draw(this_);
1771                 break;
1772         case attr_projection:
1773                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1774                         navit_projection_set(this_, attr->u.projection);
1775                         attr_updated=1;
1776                 }
1777                 break;
1778         case attr_recent_dest:
1779                 attr_updated=(this_->recentdest_count != attr->u.num);
1780                 this_->recentdest_count=attr->u.num;
1781                 break;
1782         case attr_speech:
1783                 if(this_->speech && this_->speech != attr->u.speech) {
1784                         attr_updated=1;
1785                         this_->speech = attr->u.speech;
1786                 }
1787                 break;
1788         case attr_timeout:
1789                 attr_updated=(this_->center_timeout != attr->u.num);
1790                 this_->center_timeout = attr->u.num;
1791                 break;
1792         case attr_tracking:
1793                 attr_updated=(this_->tracking_flag != !!attr->u.num);
1794                 this_->tracking_flag=!!attr->u.num;
1795                 break;
1796         case attr_use_mousewheel:
1797                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1798                 this_->use_mousewheel=!!attr->u.num;
1799                 break;
1800         case attr_vehicle:
1801                 l=this_->vehicles;
1802                 while(l) {
1803                         nv=l->data;
1804                         if (nv->vehicle == attr->u.vehicle) {
1805                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1806                                         if (this_->vehicle)
1807                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
1808                                         active.u.num=1;
1809                                         vehicle_set_attr(nv->vehicle, &active);
1810                                         attr_updated=1;
1811                                 }
1812                                 navit_set_vehicle(this_, nv);
1813                         }
1814                         l=g_list_next(l);
1815                 }
1816                 break;
1817         case attr_zoom:
1818                 zoom=transform_get_scale(this_->trans);
1819                 attr_updated=(zoom != attr->u.num);
1820                 transform_set_scale(this_->trans, attr->u.num);
1821                 if (attr_updated && !init) 
1822                         navit_draw(this_);
1823                 break;
1824         case attr_zoom_min:
1825                 attr_updated=(attr->u.num != this_->zoom_min);
1826                 this_->zoom_min=attr->u.num;
1827                 break;
1828         case attr_zoom_max:
1829                 attr_updated=(attr->u.num != this_->zoom_max);
1830                 this_->zoom_max=attr->u.num;
1831                 break;
1832         case attr_message:
1833                 navit_add_message(this_, attr->u.str);
1834                 break;
1835         case attr_follow_cursor:
1836                 attr_updated=(this_->follow_cursor != !!attr->u.num);
1837                 this_->follow_cursor=!!attr->u.num;
1838                 break;
1839         default:
1840                 return 0;
1841         }
1842         if (attr_updated && !init) {
1843                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1844                 if (attr->type == attr_osd_configuration)
1845                         graphics_draw_mode(this_->gra, draw_mode_end);
1846         }
1847         return 1;
1848 }
1849
1850 int
1851 navit_set_attr(struct navit *this_, struct attr *attr)
1852 {
1853         return navit_set_attr_do(this_, attr, 0);
1854 }
1855
1856 int
1857 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1858 {
1859         struct message *msg;
1860         int len,offset;
1861         int ret=1;
1862
1863         switch (type) {
1864         case attr_message:
1865                 msg = navit_get_messages(this_);
1866                 
1867                 if (!msg) {
1868                         return 0;
1869                 }
1870
1871                 len = 0;
1872                 while (msg) {
1873                         len += strlen(msg->text) + 1;
1874                         msg = msg->next;
1875                 }
1876                 attr->u.str = g_malloc(len + 1);
1877                 
1878                 msg = navit_get_messages(this_);
1879                 offset = 0;
1880                 while (msg) {
1881                         g_stpcpy((attr->u.str + offset), msg->text);
1882                         offset += strlen(msg->text);
1883                         attr->u.str[offset] = '\n';
1884                         offset++;
1885
1886                         msg = msg->next;
1887                 }
1888
1889                 attr->u.str[len] = '\0';
1890                 break;
1891         case attr_bookmark_map:
1892                 attr->u.map=this_->bookmark;
1893                 break;
1894         case attr_callback_list:
1895                 attr->u.callback_list=this_->attr_cbl;
1896                 break;
1897         case attr_destination:
1898                 if (! this_->destination_valid)
1899                         return 0;
1900                 attr->u.pcoord=&this_->destination;
1901                 break;
1902         case attr_displaylist:
1903                 attr->u.displaylist=this_->displaylist;
1904                 return (attr->u.displaylist != NULL);
1905         case attr_follow:
1906                 if (!this_->vehicle)
1907                         return 0;
1908                 attr->u.num=this_->vehicle->follow_curr;
1909                 break;
1910         case attr_former_destination_map:
1911                 attr->u.map=this_->former_destination;
1912                 break;
1913         case attr_graphics:
1914                 attr->u.graphics=this_->gra;
1915                 ret=(attr->u.graphics != NULL);
1916                 break;
1917         case attr_gui:
1918                 attr->u.gui=this_->gui;
1919                 ret=(attr->u.gui != NULL);
1920                 break;
1921         case attr_layout:
1922                 if (iter) {
1923                         if (iter->u.list) {
1924                                 iter->u.list=g_list_next(iter->u.list);
1925                         } else { 
1926                                 iter->u.list=this_->layouts;
1927                         }
1928                         if (!iter->u.list)
1929                                 return 0;
1930                         attr->u.layout=(struct layout *)iter->u.list->data;
1931                 } else {
1932                         attr->u.layout=this_->layout_current;
1933                 }
1934                 break;
1935         case attr_map:
1936                 if (iter && this_->mapsets) {
1937                         if (!iter->u.mapset_handle) {
1938                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1939                         }
1940                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1941                         if(!attr->u.map) {
1942                                 mapset_close(iter->u.mapset_handle);
1943                                 return 0;
1944                         }
1945                 } else {
1946                         return 0;
1947                 }
1948                 break;
1949         case attr_mapset:
1950                 attr->u.mapset=this_->mapsets->data;
1951                 ret=(attr->u.mapset != NULL);
1952                 break;
1953         case attr_navigation:
1954                 attr->u.navigation=this_->navigation;
1955                 break;
1956         case attr_orientation:
1957                 attr->u.num=this_->orientation;
1958                 break;
1959         case attr_osd_configuration:
1960                 attr->u.num=this_->osd_configuration;
1961                 break;
1962         case attr_pitch:
1963                 attr->u.num=transform_get_pitch(this_->trans);
1964                 break;
1965         case attr_projection:
1966                 if(this_->trans) {
1967                         attr->u.num=transform_get_projection(this_->trans);
1968                 } else {
1969                         return 0;
1970                 }
1971                 break;
1972         case attr_route:
1973                 attr->u.route=this_->route;
1974                 break;
1975         case attr_speech:
1976                 attr->u.speech=this_->speech;
1977                 break;
1978         case attr_tracking:
1979                 attr->u.num=this_->tracking_flag;
1980                 break;
1981         case attr_transformation:
1982                 attr->u.transformation=this_->trans;
1983                 break;
1984         case attr_vehicle:
1985                 if(iter) {
1986                         if(iter->u.list) {
1987                                 iter->u.list=g_list_next(iter->u.list);
1988                         } else { 
1989                                 iter->u.list=this_->vehicles;
1990                         }
1991                         if(!iter->u.list)
1992                                 return 0;
1993                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1994                 } else {
1995                         if(this_->vehicle) {
1996                                 attr->u.vehicle=this_->vehicle->vehicle;
1997                         } else {
1998                                 return 0;
1999                         }
2000                 }
2001                 break;
2002         case attr_zoom:
2003                 attr->u.num=transform_get_scale(this_->trans);
2004                 break;
2005         case attr_autozoom_active:
2006                 attr->u.num=this_->autozoom_active;
2007                 break;
2008         case attr_follow_cursor:
2009                 attr->u.num=this_->follow_cursor;
2010                 break;
2011         default:
2012                 return 0;
2013         }
2014         attr->type=type;
2015         return ret;
2016 }
2017
2018 static int
2019 navit_add_log(struct navit *this_, struct log *log)
2020 {
2021         struct attr type_attr;
2022         if (!log_get_attr(log, attr_type, &type_attr, NULL))
2023                 return 0;
2024         if (!strcmp(type_attr.u.str, "textfile_debug")) {
2025                 char *header = "type=track_tracked\n";
2026                 if (this_->textfile_debug_log)
2027                         return 0;
2028                 log_set_header(log, header, strlen(header));
2029                 this_->textfile_debug_log=log;
2030                 return 1;
2031         }
2032         return 0;
2033 }
2034
2035 int
2036 navit_add_attr(struct navit *this_, struct attr *attr)
2037 {
2038         int ret=1;
2039         switch (attr->type) {
2040         case attr_callback:
2041                 navit_add_callback(this_, attr->u.callback);
2042                 break;
2043         case attr_log:
2044                 ret=navit_add_log(this_, attr->u.log);
2045                 break;
2046         case attr_gui:
2047                 ret=navit_set_gui(this_, attr->u.gui);
2048                 break;
2049         case attr_graphics:
2050                 ret=navit_set_graphics(this_, attr->u.graphics);
2051                 break;
2052         case attr_layout:
2053                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2054                 if(!this_->layout_current) 
2055                         this_->layout_current=attr->u.layout;
2056                 break;
2057         case attr_route:
2058                 this_->route=attr->u.route;
2059                 break;
2060         case attr_mapset:
2061                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2062                 break;
2063         case attr_navigation:
2064                 this_->navigation=attr->u.navigation;
2065                 break;
2066         case attr_recent_dest:
2067                 this_->recentdest_count = attr->u.num;
2068                 break;
2069         case attr_speech:
2070                 this_->speech=attr->u.speech;
2071                 break;
2072         case attr_tracking:
2073                 this_->tracking=attr->u.tracking;
2074                 break;
2075         case attr_vehicle:
2076                 ret=navit_add_vehicle(this_, attr->u.vehicle);
2077                 break;
2078         case attr_vehicleprofile:
2079                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2080                 break;
2081         case attr_autozoom_min:
2082                 this_->autozoom_min = attr->u.num;
2083                 break;
2084         default:
2085                 return 0;
2086         }
2087         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2088         return ret;
2089 }
2090
2091 int
2092 navit_remove_attr(struct navit *this_, struct attr *attr)
2093 {
2094         int ret=1;
2095         switch (attr->type) {
2096         case attr_callback:
2097                 navit_remove_callback(this_, attr->u.callback);
2098                 break;
2099         default:
2100                 return 0;
2101         }
2102         return ret;
2103 }
2104
2105 struct attr_iter *
2106 navit_attr_iter_new(void)
2107 {
2108         return g_new0(struct attr_iter, 1);
2109 }
2110
2111 void
2112 navit_attr_iter_destroy(struct attr_iter *iter)
2113 {
2114         g_free(iter);
2115 }
2116
2117 void
2118 navit_add_callback(struct navit *this_, struct callback *cb)
2119 {
2120         callback_list_add(this_->attr_cbl, cb);
2121 }
2122
2123 void
2124 navit_remove_callback(struct navit *this_, struct callback *cb)
2125 {
2126         callback_list_remove(this_->attr_cbl, cb);
2127 }
2128
2129 /**
2130  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2131  *
2132  * @param navit The navit instance
2133  * @returns nothing
2134  */
2135
2136 static void
2137 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2138 {
2139         struct point cursor_pnt;
2140         enum projection pro;
2141
2142         if (this_->blocked)
2143                 return;
2144         if (pnt)
2145                 cursor_pnt=*pnt;
2146         else {
2147                 pro=transform_get_projection(this_->trans);
2148                 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2149         }
2150         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
2151 #if 0   
2152         if (pnt)
2153                 pnt2=*pnt;
2154         else {
2155                 pro=transform_get_projection(this_->trans);
2156                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2157         }
2158 #if 1
2159         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2160 #else
2161         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2162 #endif
2163 #endif
2164 }
2165
2166 static void
2167 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2168 {
2169         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2170         struct pcoord cursor_pc;
2171         struct point cursor_pnt, *pnt=&cursor_pnt;
2172         struct tracking *tracking=NULL;
2173         enum projection pro=transform_get_projection(this_->trans);
2174         int border=16;
2175         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2176         void *attr_object;
2177
2178         profile(0,NULL);
2179         if (this_->ready != 3) {
2180                 profile(0,"return 1\n");
2181                 return;
2182         }
2183         navit_layout_switch(this_);
2184         if (this_->vehicle == nv && this_->tracking_flag)
2185                 tracking=this_->tracking;
2186         if (tracking) {
2187                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2188                 attr_object=tracking;
2189                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2190         } else {
2191                 attr_object=nv->vehicle;
2192                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2193         }
2194         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2195                 if (!attr_valid.u.num != attr_position_valid_invalid)
2196                         return;
2197         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2198             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2199             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2200                 profile(0,"return 2\n");
2201                 return;
2202         }
2203         nv->dir=*attr_dir.u.numd;
2204         nv->speed=*attr_speed.u.numd;
2205         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2206         if (nv != this_->vehicle) {
2207                 navit_vehicle_draw(this_, nv, NULL);
2208                 profile(0,"return 3\n");
2209                 return;
2210         }
2211         cursor_pc.x = nv->coord.x;
2212         cursor_pc.y = nv->coord.y;
2213         cursor_pc.pro = pro;
2214         if (this_->route) {
2215                 if (tracking)
2216                         route_set_position_from_tracking(this_->route, tracking, pro);
2217                 else
2218                         route_set_position(this_->route, &cursor_pc);
2219         }
2220         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2221         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2222         if (this_->gui && nv->speed > 2)
2223                 gui_disable_suspend(this_->gui);
2224
2225         transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2226         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2227                 (nv->follow_curr == 1 || !transform_within_border(this_->trans, &cursor_pnt, border)))
2228                 navit_set_center_cursor(this_);
2229         else
2230                 navit_vehicle_draw(this_, nv, pnt);
2231
2232         if (nv->follow_curr > 1)
2233                 nv->follow_curr--;
2234         else
2235                 nv->follow_curr=nv->follow;
2236         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2237
2238         /* Finally, if we reached our destination, stop navigation. */
2239         if (this_->route && route_destination_reached(this_->route)) {
2240                 navit_set_destination(this_, NULL, NULL, 0);
2241         }
2242         profile(0,"return 5\n");
2243 }
2244
2245 /**
2246  * Set the position of the vehicle
2247  *
2248  * @param navit The navit instance
2249  * @param c The coordinate to set as position
2250  * @returns nothing
2251  */
2252
2253 void
2254 navit_set_position(struct navit *this_, struct pcoord *c)
2255 {
2256         if (this_->route) {
2257                 route_set_position(this_->route, c);
2258                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2259         }
2260         if (this_->ready == 3)
2261                 navit_draw(this_);
2262 }
2263
2264 static int
2265 navit_set_vehicleprofile(struct navit *this_, char *name)
2266 {
2267         struct attr attr;
2268         GList *l;
2269         l=this_->vehicleprofiles;
2270         while (l) {
2271                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2272                         if (!strcmp(attr.u.str, name)) {
2273                                 this_->vehicleprofile=l->data;
2274                                 if (this_->route)
2275                                         route_set_profile(this_->route, this_->vehicleprofile);
2276                                 return 1;
2277                         }
2278                 }
2279                 l=g_list_next(l);
2280         }
2281         return 0;
2282 }
2283
2284 static void
2285 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2286 {
2287         struct attr attr;
2288         this_->vehicle=nv;
2289         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2290                 if (navit_set_vehicleprofile(this_, attr.u.str))
2291                         return;
2292         }
2293         navit_set_vehicleprofile(this_,"car");
2294 }
2295
2296 /**
2297  * Register a new vehicle
2298  *
2299  * @param navit The navit instance
2300  * @param v The vehicle instance
2301  * @returns 1 for success
2302  */
2303 static int
2304 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2305 {
2306         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2307         struct attr follow, active, animate;
2308         nv->vehicle=v;
2309         nv->follow=0;
2310         nv->last.x = 0;
2311         nv->last.y = 0;
2312         nv->animate_cursor=0;
2313         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2314                 nv->follow=nv->follow=follow.u.num;
2315         nv->follow_curr=nv->follow;
2316         this_->vehicles=g_list_append(this_->vehicles, nv);
2317         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2318                 navit_set_vehicle(this_, nv);
2319         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2320                 nv->animate_cursor=animate.u.num;
2321         nv->callback.type=attr_callback;
2322         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2323         vehicle_add_attr(nv->vehicle, &nv->callback);
2324         vehicle_set_attr(nv->vehicle, &this_->self);
2325         return 1;
2326 }
2327
2328
2329
2330
2331 struct gui *
2332 navit_get_gui(struct navit *this_)
2333 {
2334         return this_->gui;
2335 }
2336
2337 struct transformation *
2338 navit_get_trans(struct navit *this_)
2339 {
2340         return this_->trans;
2341 }
2342
2343 struct route *
2344 navit_get_route(struct navit *this_)
2345 {
2346         return this_->route;
2347 }
2348
2349 struct navigation *
2350 navit_get_navigation(struct navit *this_)
2351 {
2352         return this_->navigation;
2353 }
2354
2355 struct displaylist *
2356 navit_get_displaylist(struct navit *this_)
2357 {
2358         return this_->displaylist;
2359 }
2360
2361 void
2362 navit_layout_switch(struct navit *n) 
2363 {
2364
2365     int currTs=0;
2366     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2367     double trise,tset,trise_actual;
2368     struct layout *l;
2369     int year, month, day;
2370     
2371     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2372         return; //No layout - nothing to switch
2373     }
2374     if (!n->vehicle)
2375         return;
2376     l=layout_attr.u.layout;
2377     
2378     if (l->dayname || l->nightname) {
2379         //Ok, we know that we have profile to switch
2380         
2381         //Check that we aren't calculating too fast
2382         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2383                 currTs=iso8601_to_secs(iso8601_attr.u.str);
2384                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2385         }
2386         if (currTs-(n->prevTs)<60) {
2387             //We've have to wait a little
2388             return;
2389         }
2390         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2391                 return;
2392         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2393                 //No position - no sun
2394                 return;
2395         }
2396         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2397                 return; //No valid fix yet
2398         }
2399         
2400         //We calculate sunrise anyway, cause it is needed both for day and for night
2401         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,35,1,&trise,&tset)!=0) {
2402                 //near the pole sun never rises/sets, so we should never switch profiles
2403                 n->prevTs=currTs;
2404                 return;
2405             }
2406         
2407         trise_actual=trise;
2408         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2409         if (l->dayname) {
2410         
2411             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
2412                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2413                 //The sun is rising now!
2414                 if (strcmp(l->name,l->dayname)) {
2415                     navit_set_layout_by_name(n,l->dayname);
2416                 }
2417             }
2418         }
2419         if (l->nightname) {
2420             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,-24,0,&trise,&tset)!=0) {
2421                 //near the pole sun never rises/sets, so we should never switch profiles
2422                 n->prevTs=currTs;
2423                 return;
2424             }
2425             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2426             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2427                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
2428                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2429                 //Time to sleep
2430                 if (strcmp(l->name,l->nightname)) {
2431                     navit_set_layout_by_name(n,l->nightname);
2432                 }
2433             }   
2434         }
2435         
2436         n->prevTs=currTs;
2437     }
2438 }
2439
2440 int 
2441 navit_set_layout_by_name(struct navit *n,char *name) 
2442 {
2443     struct layout *l;
2444     struct attr_iter iter;
2445     struct attr layout_attr;
2446
2447     iter.u.list=0x00;
2448
2449     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2450         return 0; //No layouts - nothing to do
2451     }
2452     if (iter.u.list==NULL) {
2453         return 0;
2454     }
2455     
2456     iter.u.list=g_list_first(iter.u.list);
2457     
2458     while(iter.u.list) {
2459         l=(struct layout*)iter.u.list->data;
2460         if (!strcmp(name,l->name)) {
2461             layout_attr.u.layout=l;
2462             layout_attr.type=attr_layout;
2463             navit_set_attr(n,&layout_attr);
2464             iter.u.list=g_list_first(iter.u.list);
2465             return 1;
2466         }
2467         iter.u.list=g_list_next(iter.u.list);
2468     }
2469
2470     iter.u.list=g_list_first(iter.u.list);
2471     return 0;
2472 }
2473
2474 int
2475 navit_block(struct navit *this_, int block)
2476 {
2477         if (block) {
2478                 this_->blocked |= 1;
2479                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2480                         this_->blocked |= 2;
2481                 return 0;
2482         }
2483         if (this_->blocked & 2) {
2484                 this_->blocked=0;
2485                 navit_draw(this_);
2486                 return 1;
2487         }
2488         this_->blocked=0;
2489         return 0;
2490 }
2491
2492 void
2493 navit_destroy(struct navit *this_)
2494 {
2495         /* TODO: destroy objects contained in this_ */
2496         if (this_->vehicle)
2497                 vehicle_destroy(this_->vehicle->vehicle);
2498         char *center_file = navit_get_center_file(TRUE);
2499         navit_write_center_to_file(this_, center_file);
2500         g_free(center_file);
2501         callback_destroy(this_->nav_speech_cb);
2502         callback_destroy(this_->roadbook_callback);
2503         callback_destroy(this_->popup_callback);
2504         callback_destroy(this_->motion_timeout_callback);
2505         if(this_->gra)
2506           graphics_remove_callback(this_->gra, this_->resize_callback);
2507         callback_destroy(this_->resize_callback);
2508         if(this_->gra)
2509           graphics_remove_callback(this_->gra, this_->button_callback);
2510         callback_destroy(this_->button_callback);
2511         if(this_->gra)
2512           graphics_remove_callback(this_->gra, this_->motion_callback);
2513         callback_destroy(this_->motion_callback);
2514         g_free(this_);
2515 }
2516
2517 /** @} */