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