Fix:Android:Googlenavigation support now really works fine, and small cleanups
[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_mode(this_->gra, draw_mode_begin);
272                         graphics_draw_text_std(this_->gra, 16, str, &p);
273                         g_free(str);
274                         p.y+=32;
275                         graphics_draw_mode(this_->gra, draw_mode_end);
276                 }
277         }
278         mapset_close(msh);
279 }
280
281 static void
282 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
283 {
284         int updated;
285         if (attr->type != attr_route_status)
286                 return;
287         updated=attr->u.num;
288         if (this_->ready != 3)
289                 return;
290         if (updated != route_status_path_done_new)
291                 return;
292         if (this_->vehicle) {
293                 if (this_->vehicle->follow_curr == 1)
294                         return;
295                 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
296                         this_->vehicle->follow_curr=this_->vehicle->follow;
297         }
298         navit_draw(this_);
299 }
300
301 void
302 navit_handle_resize(struct navit *this_, int w, int h)
303 {
304         struct map_selection sel;
305         int callback=(this_->ready == 1);
306         this_->ready |= 2;
307         memset(&sel, 0, sizeof(sel));
308         this_->w=w;
309         this_->h=h;
310         sel.u.p_rect.rl.x=w;
311         sel.u.p_rect.rl.y=h;
312         transform_set_screen_selection(this_->trans, &sel);
313         graphics_init(this_->gra);
314         graphics_set_rect(this_->gra, &sel.u.p_rect);
315         if (callback) 
316                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
317         if (this_->ready == 3)
318                 navit_draw_async(this_, 1);
319 }
320
321 static void
322 navit_resize(void *data, int w, int h)
323 {
324         struct navit *this=data;
325         if (!this->ignore_graphics_events)
326                 navit_handle_resize(this, w, h);
327 }
328
329 int
330 navit_get_width(struct navit *this_)
331 {
332         return this_->w;
333 }
334
335
336 int
337 navit_get_height(struct navit *this_)
338 {
339         return this_->h;
340 }
341
342 static void
343 navit_popup(void *data)
344 {
345         struct navit *this_=data;
346         popup(this_, 1, &this_->pressed);
347         this_->button_timeout=NULL;
348         this_->popped=1;
349 }
350
351
352 int
353 navit_ignore_button(struct navit *this_)
354 {
355         if (this_->ignore_button)
356                 return 1;
357         this_->ignore_button=1;
358         return 0;
359 }
360
361 void
362 navit_ignore_graphics_events(struct navit *this_, int ignore)
363 {
364         this_->ignore_graphics_events=ignore;
365 }
366
367 static void
368 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
369 {
370         struct coord co,cn;
371         struct coord c,*cp;
372         int yaw;
373         double angleo,anglen;
374
375         if (!transform_reverse(tr, old, &co))
376                 return;
377         if (rot) {
378                 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
379                 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
380                 yaw=transform_get_yaw(tr)+angleo-anglen;
381                 transform_set_yaw(tr, yaw % 360);
382         }
383         if (!transform_reverse(tr, new, &cn))
384                 return;
385         cp=transform_get_center(tr);
386         c.x=cp->x+co.x-cn.x;
387         c.y=cp->y+co.y-cn.y;
388         dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
389         transform_set_center(tr, &c);
390 }
391
392 void
393 navit_set_timeout(struct navit *this_)
394 {
395         struct attr follow;
396         follow.type=attr_follow;
397         follow.u.num=this_->center_timeout;
398         navit_set_attr(this_, &follow);
399 }
400
401 int
402 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
403 {
404         int border=16;
405
406         dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
407         callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
408         if (this_->ignore_button) {
409                 this_->ignore_button=0;
410                 return 0;
411         }
412         if (pressed) {
413                 this_->pressed=*p;
414                 this_->last=*p;
415                 this_->zoomed=0;
416                 if (button == 1) {
417                         this_->button_pressed=1;
418                         this_->moved=0;
419                         this_->popped=0;
420                         if (popup_callback)
421                                 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
422                 }
423                 if (button == 2)
424                         navit_set_center_screen(this_, p, 1);
425                 if (button == 3)
426                         popup(this_, button, p);
427                 if (button == 4 && this_->use_mousewheel) {
428                         this_->zoomed = 1;
429                         navit_zoom_in(this_, 2, p);
430                 }
431                 if (button == 5 && this_->use_mousewheel) {
432                         this_->zoomed = 1;
433                         navit_zoom_out(this_, 2, p);
434                 }
435         } else {
436
437                 this_->button_pressed=0;
438                 if (this_->button_timeout) {
439                         event_remove_timeout(this_->button_timeout);
440                         this_->button_timeout=NULL;
441                         if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
442                                 navit_set_center_screen(this_, p, !this_->zoomed);
443                         }
444                 }
445                 if (this_->motion_timeout) {
446                         event_remove_timeout(this_->motion_timeout);
447                         this_->motion_timeout=NULL;
448                 }
449                 if (this_->moved) {
450                         struct point pr;
451                         pr.x=this_->w/2;
452                         pr.y=this_->h;
453 #if 0
454                         update_transformation(this_->trans, &this_->pressed, p, &pr);
455 #else
456                         update_transformation(this_->trans, &this_->pressed, p, NULL);
457 #endif
458                         graphics_draw_drag(this_->gra, NULL);
459                         transform_copy(this_->trans, this_->trans_cursor);
460                         graphics_overlay_disable(this_->gra, 0);
461                         if (!this_->zoomed) 
462                                 navit_set_timeout(this_);
463                         navit_draw(this_);
464                 } else
465                         return 1;
466         }
467         return 0;
468 }
469
470 static void
471 navit_button(void *data, int pressed, int button, struct point *p)
472 {
473         struct navit *this=data;
474         dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
475         if (!this->ignore_graphics_events) {
476                 if (! this->popup_callback)
477                         this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
478                 navit_handle_button(this, pressed, button, p, this->popup_callback);
479         }
480 }
481
482
483 static void
484 navit_motion_timeout(struct navit *this_)
485 {
486         int dx, dy;
487
488         if (this_->drag_bitmap) {
489                 struct point point;
490                 point.x=(this_->current.x-this_->pressed.x);
491                 point.y=(this_->current.y-this_->pressed.y);
492                 if (graphics_draw_drag(this_->gra, &point)) {
493                         graphics_overlay_disable(this_->gra, 1);
494                         graphics_draw_mode(this_->gra, draw_mode_end);
495                         this_->moved=1;
496                         this_->motion_timeout=NULL;
497                         return;
498                 }
499         } 
500         dx=(this_->current.x-this_->last.x);
501         dy=(this_->current.y-this_->last.y);
502         if (dx || dy) {
503                 struct transformation *tr;
504                 struct point pr;
505                 this_->last=this_->current;
506                 graphics_overlay_disable(this_->gra, 1);
507                 tr=transform_dup(this_->trans);
508                 pr.x=this_->w/2;
509                 pr.y=this_->h;
510 #if 0
511                 update_transformation(tr, &this_->pressed, &this_->current, &pr);
512 #else
513                 update_transformation(tr, &this_->pressed, &this_->current, NULL);
514 #endif
515 #if 0
516                 graphics_displaylist_move(this_->displaylist, dx, dy);
517 #endif
518                 graphics_draw_cancel(this_->gra, this_->displaylist);
519                 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
520                 transform_destroy(tr);
521                 this_->moved=1;
522         }
523         this_->motion_timeout=NULL;
524         return;
525 }
526
527 void
528 navit_handle_motion(struct navit *this_, struct point *p)
529 {
530         int dx, dy;
531
532         if (this_->button_pressed && !this_->popped) {
533                 dx=(p->x-this_->pressed.x);
534                 dy=(p->y-this_->pressed.y);
535                 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
536                         this_->moved=1;
537                         if (this_->button_timeout) {
538                                 event_remove_timeout(this_->button_timeout);
539                                 this_->button_timeout=NULL;
540                         }
541                         this_->current=*p;
542                         if (! this_->motion_timeout_callback)
543                                 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
544                         if (! this_->motion_timeout)
545                                 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
546                 }
547         }
548 }
549
550 static void
551 navit_motion(void *data, struct point *p)
552 {
553         struct navit *this=data;
554         if (!this->ignore_graphics_events) 
555                 navit_handle_motion(this, p);
556 }
557
558 static void
559 navit_predraw(struct navit *this_)
560 {
561         GList *l;
562         struct navit_vehicle *nv;
563         transform_copy(this_->trans, this_->trans_cursor);
564         l=this_->vehicles;
565         while (l) {
566                 nv=l->data;
567                 navit_vehicle_draw(this_, nv, NULL);
568                 l=g_list_next(l);
569         }
570 }
571
572 static void
573 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
574 {
575         struct coord c1, c2, *center;
576         if (scale < this_->zoom_min)
577                 scale=this_->zoom_min;
578         if (scale > this_->zoom_max)
579                 scale=this_->zoom_max;
580         if (p)
581                 transform_reverse(this_->trans, p, &c1);
582         transform_set_scale(this_->trans, scale);
583         if (p) {
584                 transform_reverse(this_->trans, p, &c2);
585                 center = transform_center(this_->trans);
586                 center->x += c1.x - c2.x;
587                 center->y += c1.y - c2.y;
588         }
589         if (draw)
590                 navit_draw(this_);
591 }
592
593 /**
594  * @brief Automatically adjusts zoom level
595  *
596  * This function automatically adjusts the current
597  * zoom level according to the current speed.
598  *
599  * @param this_ The navit struct
600  * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
601  * @param speed The vehicles speed in meters per second
602  * @param dir The direction into which the vehicle moves
603  */
604 static void
605 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
606 {
607         struct point pc;
608         int distance,w,h;
609         double new_scale;
610         long scale;
611
612         if (! this_->autozoom_active) {
613                 return;
614         }
615
616         distance = speed * this_->autozoom_secs;
617
618         transform_get_size(this_->trans, &w, &h);
619         transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
620         scale = transform_get_scale(this_->trans);
621
622         /* We make sure that the point we want to see is within a certain range
623          * around the vehicle. The radius of this circle is the size of the
624          * screen. This doesn't necessarily mean the point is visible because of
625          * perspective etc. Quite rough, but should be enough. */
626         
627         if (w > h) {
628                 new_scale = (double)distance / h * 16; 
629         } else {
630                 new_scale = (double)distance / w * 16; 
631         }
632
633         if (abs(new_scale - scale) < 2) { 
634                 return; // Smoothing
635         }
636         
637         if (new_scale >= this_->autozoom_min) {
638                 navit_scale(this_, (long)new_scale, &pc, 0);
639         } else {
640                 if (scale != this_->autozoom_min) {
641                         navit_scale(this_, this_->autozoom_min, &pc, 0);
642                 }
643         }
644 }
645
646 /**
647  * Change the current zoom level, zooming closer to the ground
648  *
649  * @param navit The navit instance
650  * @param factor The zoom factor, usually 2
651  * @param p The invariant point (if set to NULL, default to center)
652  * @returns nothing
653  */
654 void
655 navit_zoom_in(struct navit *this_, int factor, struct point *p)
656 {
657         long scale=transform_get_scale(this_->trans)/factor;
658         if (scale < 1)
659                 scale=1;
660         navit_scale(this_, scale, p, 1);
661 }
662
663 /**
664  * Change the current zoom level
665  *
666  * @param navit The navit instance
667  * @param factor The zoom factor, usually 2
668  * @param p The invariant point (if set to NULL, default to center)
669  * @returns nothing
670  */
671 void
672 navit_zoom_out(struct navit *this_, int factor, struct point *p)
673 {
674         long scale=transform_get_scale(this_->trans)*factor;
675         navit_scale(this_, scale, p, 1);
676 }
677
678 void
679 navit_zoom_in_cursor(struct navit *this_, int factor)
680 {
681         struct point p;
682         if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
683                 navit_zoom_in(this_, factor, &p);
684                 this_->vehicle->follow_curr=this_->vehicle->follow;
685         } else
686                 navit_zoom_in(this_, factor, NULL);
687 }
688
689 void
690 navit_zoom_out_cursor(struct navit *this_, int factor)
691 {
692         struct point p;
693         if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
694                 navit_zoom_out(this_, 2, &p);
695                 this_->vehicle->follow_curr=this_->vehicle->follow;
696         } else
697                 navit_zoom_out(this_, 2, NULL);
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
1079             dbg(1, "navit->navit_set_destination %i\n", c->x);
1080             dbg(1, "navit->navit_set_destination %i\n", c->y);
1081
1082         } else
1083                 this_->destination_valid=0;
1084         char *destination_file = bookmarks_get_destination_file(TRUE);
1085         bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1086         g_free(destination_file);
1087         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1088         if (this_->route) {
1089                 route_set_destination(this_->route, c, async);
1090
1091                 if (this_->ready == 3)
1092                         navit_draw(this_);
1093         }
1094 }
1095
1096 /**
1097  * Start the route computing to a given set of coordinates including waypoints
1098  *
1099  * @param navit The navit instance
1100  * @param c The coordinate to start routing to
1101  * @param description A label which allows the user to later identify this destination in the former destinations selection
1102  * @returns nothing
1103  */
1104 void
1105 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1106 {
1107         if (c && count) {
1108                 this_->destination=c[count-1];
1109                 this_->destination_valid=1;
1110         } else
1111                 this_->destination_valid=0;
1112         char *destination_file = bookmarks_get_destination_file(TRUE);
1113         bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1114         g_free(destination_file);
1115         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1116         if (this_->route) {
1117                 route_set_destinations(this_->route, c, count, async);
1118
1119                 if (this_->ready == 3)
1120                         navit_draw(this_);
1121         }
1122 }
1123
1124 /**
1125  * @brief Checks if a route is calculated
1126  *
1127  * This function checks if a route is calculated.
1128  *
1129  * @param this_ The navit struct whose route should be checked.
1130  * @return True if the route is set, false otherwise.
1131  */
1132 int
1133 navit_check_route(struct navit *this_)
1134 {
1135         if (this_->route) {
1136                 return route_get_path_set(this_->route);
1137         }
1138
1139         return 0;
1140 }
1141
1142 static int
1143 navit_former_destinations_active(struct navit *this_)
1144 {
1145         char *destination_file = bookmarks_get_destination_file(FALSE);
1146         FILE *f;
1147         int active=0;
1148         char buffer[3];
1149         f=fopen(destination_file,"r");
1150         if (f) {
1151                 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n')) 
1152                         active=1;
1153                 fclose(f);
1154         }
1155         g_free(destination_file);
1156         return active;
1157 }
1158
1159 static void
1160 navit_add_former_destinations_from_file(struct navit *this_)
1161 {
1162         char *destination_file = bookmarks_get_destination_file(FALSE);
1163         struct attr parent={attr_navit, .u.navit=this_};
1164         struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}}, flags={attr_flags, {(void *)1}};
1165         struct attr *attrs[]={&type, &data, &flags, NULL};
1166         struct map_rect *mr;
1167         struct item *item;
1168         int i,valid=0,count=0;
1169         struct coord c[16];
1170         struct pcoord pc[16];
1171
1172         this_->former_destination=map_new(&parent, attrs);
1173         g_free(destination_file);
1174         if (!this_->route || !navit_former_destinations_active(this_))
1175                 return; 
1176         mr=map_rect_new(this_->former_destination, NULL);
1177         while ((item=map_rect_get_item(mr))) {
1178                 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16))) 
1179                         valid=1;
1180         }
1181         map_rect_destroy(mr);
1182         if (valid && count > 0) {
1183                 for (i = 0 ; i < count ; i++) {
1184                         pc[i].pro=map_projection(this_->former_destination);
1185                         pc[i].x=c[i].x;
1186                         pc[i].y=c[i].y;
1187                 }
1188                 if (count == 1)
1189                         route_set_destination(this_->route, &pc[0], 1);
1190                 else
1191                         route_set_destinations(this_->route, pc, count, 1);
1192                 this_->destination=pc[count-1];
1193                 this_->destination_valid=1;
1194         }
1195 }
1196
1197
1198 void
1199 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1200 {
1201         va_list ap;
1202         char *str1,*str2;
1203         va_start(ap, fmt);
1204         if (this_->textfile_debug_log && this_->vehicle) {
1205                 str1=g_strdup_vprintf(fmt, ap);
1206                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1207                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1208                 g_free(str2);
1209                 g_free(str1);
1210         }
1211         va_end(ap);
1212 }
1213
1214 void
1215 navit_say(struct navit *this_, char *text)
1216 {
1217         if(this_->speech) {
1218                 speech_say(this_->speech, text);
1219         }
1220 }
1221
1222 /**
1223  * @brief Toggles the navigation announcer for navit
1224  * @param this_ The navit object
1225  */
1226 static void
1227 navit_cmd_announcer_toggle(struct navit *this_)
1228 {
1229     struct attr attr, speechattr;
1230
1231     // search for the speech attribute
1232     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1233         return;
1234     // find out if the corresponding attribute attr_active has been set
1235     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1236         // flip it then...
1237         attr.u.num = !attr.u.num;
1238     } else {
1239         // otherwise disable it because voice is enabled by default
1240         attr.type = attr_active;
1241         attr.u.num = 0;
1242     }
1243
1244     // apply the new state
1245     if(!speech_set_attr(speechattr.u.speech, &attr))
1246         return;
1247
1248     // announce that the speech attribute has changed
1249     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1250 }
1251
1252 void
1253 navit_speak(struct navit *this_)
1254 {
1255         struct navigation *nav=this_->navigation;
1256         struct map *map=NULL;
1257         struct map_rect *mr=NULL;
1258         struct item *item;
1259         struct attr attr;
1260
1261     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1262         attr.u.num = 1;
1263     dbg(1, "this_.speech->active %i\n", attr.u.num);
1264     if(!attr.u.num)
1265         return;
1266
1267         if (nav)
1268                 map=navigation_get_map(nav);
1269         if (map)
1270                 mr=map_rect_new(map, NULL);
1271         if (mr) {
1272                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1273                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1274                         speech_say(this_->speech, attr.u.str);
1275                         navit_add_message(this_, attr.u.str);
1276                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1277                 }
1278                 map_rect_destroy(mr);
1279         }
1280 }
1281
1282 static void
1283 navit_window_roadbook_update(struct navit *this_)
1284 {
1285         struct navigation *nav=this_->navigation;
1286         struct map *map=NULL;
1287         struct map_rect *mr=NULL;
1288         struct item *item;
1289         struct attr attr;
1290         struct param_list param[5];
1291         int secs;
1292
1293         dbg(1,"enter\n");
1294         datawindow_mode(this_->roadbook_window, 1);
1295         if (nav)
1296                 map=navigation_get_map(nav);
1297         if (map)
1298                 mr=map_rect_new(map, NULL);
1299         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1300         if (mr) {
1301                 dbg(0,"while loop\n");
1302                 while ((item=map_rect_get_item(mr))) {
1303                         dbg(0,"item=%p\n", item);
1304                         attr.u.str=NULL;
1305                         if (item->type != type_nav_position) {
1306                                 item_attr_get(item, attr_navigation_long, &attr);
1307                                 if (attr.u.str == NULL) {
1308                                         continue;
1309                                 }
1310                                 dbg(2, "Command='%s'\n", attr.u.str);
1311                                 param[0].value=g_strdup(attr.u.str);
1312                         } else
1313                                 param[0].value=_("Position");
1314                         param[0].name=_("Command");
1315
1316                         item_attr_get(item, attr_length, &attr);
1317                         dbg(2, "Length=%d\n", attr.u.num);
1318                         param[1].name=_("Length");
1319
1320                         if ( attr.u.num >= 2000 )
1321                         {
1322                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1323                         }
1324                         else
1325                         {
1326                                 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1327                         }
1328
1329                         item_attr_get(item, attr_time, &attr);
1330                         dbg(2, "Time=%d\n", attr.u.num);
1331                         secs=attr.u.num/10;
1332                         param[2].name=_("Time");
1333                         if ( secs >= 3600 )
1334                         {
1335                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1336                         }
1337                         else
1338                         {
1339                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1340                         }
1341
1342                         item_attr_get(item, attr_destination_length, &attr);
1343                         dbg(2, "Destlength=%d\n", attr.u.num);
1344                         param[3].name=_("Destination Length");
1345                         if ( attr.u.num >= 2000 )
1346                         {
1347                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1348                         }
1349                         else
1350                         {
1351                                 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1352                         }
1353
1354                         item_attr_get(item, attr_destination_time, &attr);
1355                         dbg(2, "Desttime=%d\n", attr.u.num);
1356                         secs=attr.u.num/10;
1357                         param[4].name=_("Destination Time");
1358                         if ( secs >= 3600 )
1359                         {
1360                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1361                         }
1362                         else
1363                         {
1364                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1365                         }
1366                         datawindow_add(this_->roadbook_window, param, 5);
1367                 }
1368                 map_rect_destroy(mr);
1369         }
1370         datawindow_mode(this_->roadbook_window, 0);
1371 }
1372
1373 void
1374 navit_window_roadbook_destroy(struct navit *this_)
1375 {
1376         dbg(0, "enter\n");
1377         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1378         this_->roadbook_window=NULL;
1379         this_->roadbook_callback=NULL;
1380 }
1381 void
1382 navit_window_roadbook_new(struct navit *this_)
1383 {
1384         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1385                 return;
1386         }
1387
1388         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1389         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1390         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1391         navit_window_roadbook_update(this_);
1392 }
1393
1394 void
1395 navit_init(struct navit *this_)
1396 {
1397         struct mapset *ms;
1398         struct map *map;
1399         int callback;
1400
1401         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1402
1403         if (!this_->gui && !(this_->flags & 2)) {
1404                 dbg(0,"no gui\n");
1405                 navit_destroy(this_);
1406                 return;
1407         }
1408         if (!this_->gra && !(this_->flags & 1)) {
1409                 dbg(0,"no graphics\n");
1410                 navit_destroy(this_);
1411                 return;
1412         }
1413         dbg(2,"Connecting gui to graphics\n");
1414         if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1415                 struct attr attr_type_gui, attr_type_graphics;
1416                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1417                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1418                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1419                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1420                 dbg(0," for explanations and solutions\n");
1421
1422                 navit_destroy(this_);
1423                 return;
1424         }
1425         if (this_->speech && this_->navigation) {
1426                 struct attr speech;
1427                 speech.type=attr_speech;
1428                 speech.u.speech=this_->speech;
1429                 navigation_set_attr(this_->navigation, &speech);
1430         }
1431         dbg(2,"Initializing graphics\n");
1432         dbg(2,"Setting Vehicle\n");
1433         navit_set_vehicle(this_, this_->vehicle);
1434         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1435         if (this_->mapsets) {
1436                 struct mapset_handle *msh;
1437                 ms=this_->mapsets->data;
1438                 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1439                 msh=mapset_open(ms);
1440                 while (msh && (map=mapset_next(msh, 0))) {
1441                         map_add_callback(map, this_->progress_cb);
1442                 }
1443                 mapset_close(msh);
1444                 
1445                 if (this_->route) {
1446                         if ((map=route_get_map(this_->route)))
1447                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1448                         if ((map=route_get_graph_map(this_->route))) {
1449                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1450                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1451                         }
1452                         route_set_mapset(this_->route, ms);
1453                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1454                 }
1455                 if (this_->tracking) {
1456                         tracking_set_mapset(this_->tracking, ms);
1457                         if (this_->route)
1458                                 tracking_set_route(this_->tracking, this_->route);
1459                 }
1460                 if (this_->navigation) {
1461                         if ((map=navigation_get_map(this_->navigation))) {
1462                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1463                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1464                         }
1465                 }
1466                 if (this_->tracking) {
1467                         if ((map=tracking_get_map(this_->tracking))) {
1468                                 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1469                                 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1470                         }
1471                 }
1472                 navit_add_former_destinations_from_file(this_);
1473         }
1474         if (this_->route) {
1475                 struct attr callback;
1476                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1477                 callback.type=attr_callback;
1478                 callback.u.callback=this_->route_cb;
1479                 route_add_attr(this_->route, &callback);
1480         }
1481         if (this_->navigation) {
1482                 if (this_->speech) {
1483                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1484                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1485                 }
1486                 if (this_->route)
1487                         navigation_set_route(this_->navigation, this_->route);
1488         }
1489         dbg(2,"Setting Center\n");
1490         char *center_file = bookmarks_get_center_file(FALSE);
1491         bookmarks_set_center_from_file(this_->bookmarks, center_file);
1492         g_free(center_file);
1493 #if 0
1494         if (this_->menubar) {
1495                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1496                 if (men) {
1497                         navit_add_menu_windows_items(this_, men);
1498                 }
1499         }
1500 #endif
1501         global_navit=this_;
1502 #if 0
1503         navit_window_roadbook_new(this_);
1504         navit_window_items_new(this_);
1505 #endif
1506
1507         messagelist_init(this_->messages);
1508
1509         navit_set_cursors(this_);
1510
1511         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1512         callback=(this_->ready == 2);
1513         this_->ready|=1;
1514         dbg(2,"ready=%d\n",this_->ready);
1515         if (this_->ready == 3)
1516                 navit_draw_async(this_, 1);
1517         if (callback)
1518                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1519 #if 0
1520         routech_test(this_);
1521 #endif
1522 }
1523
1524 void
1525 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1526 {
1527         struct coord c;
1528         int scale=16;
1529
1530         c.x=(r->rl.x+r->lu.x)/2;
1531         c.y=(r->rl.y+r->lu.y)/2;
1532         transform_set_center(this_->trans, &c);
1533         dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1534         while (scale < 1<<20) {
1535                 struct point p1,p2;
1536                 transform_set_scale(this_->trans, scale);
1537                 transform_setup_source_rect(this_->trans);
1538                 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1539                 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1540                 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1541                 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1542                     p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1543                         scale*=2;
1544                 else
1545                         break;
1546         
1547         }
1548         if (this_->ready == 3)
1549                 navit_draw_async(this_,0);
1550 }
1551
1552 void
1553 navit_zoom_to_route(struct navit *this_, int orientation)
1554 {
1555         struct map *map;
1556         struct map_rect *mr=NULL;
1557         struct item *item;
1558         struct coord c;
1559         struct coord_rect r;
1560         int count=0;
1561         if (! this_->route)
1562                 return;
1563         dbg(1,"enter\n");
1564         map=route_get_map(this_->route);
1565         dbg(1,"map=%p\n",map);
1566         if (map)
1567                 mr=map_rect_new(map, NULL);
1568         dbg(1,"mr=%p\n",mr);
1569         if (mr) {
1570                 while ((item=map_rect_get_item(mr))) {
1571                         dbg(1,"item=%s\n", item_to_name(item->type));
1572                         while (item_coord_get(item, &c, 1)) {
1573                                 dbg(1,"coord\n");
1574                                 if (!count) 
1575                                         r.lu=r.rl=c;
1576                                 else
1577                                         coord_rect_extend(&r, &c);      
1578                                 count++;
1579                         }
1580                 }
1581         }
1582         if (! count)
1583                 return;
1584         if (orientation != -1)
1585                 transform_set_yaw(this_->trans, orientation);
1586         navit_zoom_to_rect(this_, &r);
1587 }
1588
1589 static void
1590 navit_cmd_zoom_to_route(struct navit *this)
1591 {
1592         navit_zoom_to_route(this, 0);
1593 }
1594
1595
1596 /**
1597  * Change the current zoom level
1598  *
1599  * @param navit The navit instance
1600  * @param center The point where to center the map, including its projection
1601  * @returns nothing
1602  */
1603 void
1604 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1605 {
1606         struct coord *c=transform_center(this_->trans);
1607         struct coord c1,c2;
1608         enum projection pro = transform_get_projection(this_->trans);
1609         if (pro != center->pro) {
1610                 c1.x = center->x;
1611                 c1.y = center->y;
1612                 transform_from_to(&c1, center->pro, &c2, pro);
1613         } else {
1614                 c2.x = center->x;
1615                 c2.y = center->y;
1616         }
1617         *c=c2;
1618         if (set_timeout) 
1619                 navit_set_timeout(this_);
1620         if (this_->ready == 3)
1621                 navit_draw(this_);
1622 }
1623
1624 static void
1625 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1626 {
1627         int width, height;
1628         struct point po;
1629         transform_set_center(this_->trans, c);
1630         transform_get_size(this_->trans, &width, &height);
1631         po.x=width/2;
1632         po.y=height/2;
1633         update_transformation(this_->trans, &po, p, NULL);
1634         if (set_timeout)
1635                 navit_set_timeout(this_);
1636 }
1637
1638 /**
1639  * Links all vehicles to a cursor depending on the current profile.
1640  *
1641  * @param this_ A navit instance
1642  * @author Ralph Sennhauser (10/2009)
1643  */
1644 static void
1645 navit_set_cursors(struct navit *this_)
1646 {
1647         struct attr name;
1648         struct navit_vehicle *nv;
1649         struct cursor *c;
1650         GList *v;
1651
1652         v=g_list_first(this_->vehicles); // GList of navit_vehicles
1653         while (v) {
1654                 nv=v->data;
1655                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1656                         if (!strcmp(name.u.str,"none"))
1657                                 c=NULL;
1658                         else
1659                                 c=layout_get_cursor(this_->layout_current, name.u.str);
1660                 } else
1661                         c=layout_get_cursor(this_->layout_current, "default");
1662                 vehicle_set_cursor(nv->vehicle, c);
1663                 v=g_list_next(v);
1664         }
1665         return;
1666 }
1667
1668 static int
1669 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1670 {
1671         int width, height;
1672         struct navit_vehicle *nv=this_->vehicle;
1673
1674         float offset=this_->radius;      // Cursor offset from the center of the screen (percent).
1675 #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 */
1676         float min_offset = 0.;      // Percent offset at min_offset_speed.
1677         float max_offset = 30.;     // Percent offset at max_offset_speed.
1678         int min_offset_speed = 2;   // Speed in km/h
1679         int max_offset_speed = 50;  // Speed ini km/h
1680         // Calculate cursor offset from the center of the screen, upon speed.
1681         if (nv->speed <= min_offset_speed) {
1682             offset = min_offset;
1683         } else if (nv->speed > max_offset_speed) {
1684             offset = max_offset;
1685         } else {
1686             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1687         }
1688 #endif
1689
1690         transform_get_size(this_->trans, &width, &height);
1691         if (this_->orientation == -1 || keep_orientation) {
1692                 p->x=50*width/100;
1693                 p->y=(50 + offset)*height/100;
1694                 if (dir) 
1695                         *dir=keep_orientation?this_->orientation:nv->dir;
1696         } else {
1697                 int mdir;
1698                 if (this_->tracking && this_->tracking_flag) {
1699                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1700                 } else {
1701                         mdir=nv->dir-this_->orientation;
1702                 }
1703
1704                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1705                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1706                 if (dir)
1707                         *dir=this_->orientation;
1708         }
1709         return 1;
1710 }
1711
1712 void
1713 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1714 {
1715         int dir;
1716         struct point pn;
1717         struct navit_vehicle *nv=this_->vehicle;
1718         navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1719         transform_set_yaw(this_->trans, dir);
1720         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1721         if (autozoom)
1722                 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1723 }
1724
1725 static void
1726 navit_set_center_cursor_draw(struct navit *this_)
1727 {
1728         navit_set_center_cursor(this_,1,0);
1729         if (this_->ready == 3)
1730                 navit_draw_async(this_, 1);
1731 }
1732
1733 static void
1734 navit_cmd_set_center_cursor(struct navit *this_)
1735 {
1736         navit_set_center_cursor_draw(this_);
1737 }
1738
1739 void
1740 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1741 {
1742         struct coord c;
1743         struct pcoord pc;
1744         transform_reverse(this_->trans, p, &c);
1745         pc.x = c.x;
1746         pc.y = c.y;
1747         pc.pro = transform_get_projection(this_->trans);
1748         navit_set_center(this_, &pc, set_timeout);
1749 }
1750
1751 #if 0
1752                 switch((*attrs)->type) {
1753                 case attr_zoom:
1754                         zoom=(*attrs)->u.num;
1755                         break;
1756                 case attr_center:
1757                         g=*((*attrs)->u.coord_geo);
1758                         break;
1759 #endif
1760
1761 static int
1762 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1763 {
1764         int dir=0, orient_old=0, attr_updated=0;
1765         struct coord co;
1766         long zoom;
1767         GList *l;
1768         struct navit_vehicle *nv;
1769         struct attr active=(struct attr){attr_active,{(void *)0}};
1770         struct layout *lay;
1771
1772         switch (attr->type) {
1773         case attr_autozoom:
1774                 attr_updated=(this_->autozoom_secs != attr->u.num);
1775                 this_->autozoom_secs = attr->u.num;
1776                 break;
1777         case attr_autozoom_active:
1778                 attr_updated=(this_->autozoom_active != attr->u.num);
1779                 this_->autozoom_active = attr->u.num;
1780                 break;
1781         case attr_center:
1782                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1783                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1784                 transform_set_center(this_->trans, &co);
1785                 break;
1786         case attr_drag_bitmap:
1787                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1788                 this_->drag_bitmap=!!attr->u.num;
1789                 break;
1790         case attr_flags:
1791                 attr_updated=(this_->flags != attr->u.num);
1792                 this_->flags=attr->u.num;
1793                 break;
1794         case attr_flags_graphics:
1795                 attr_updated=(this_->graphics_flags != attr->u.num);
1796                 this_->graphics_flags=attr->u.num;
1797                 break;
1798         case attr_follow:
1799                 if (!this_->vehicle)
1800                         return 0;
1801                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1802                 this_->vehicle->follow_curr = attr->u.num;
1803                 break;
1804         case attr_layout:
1805                 if(this_->layout_current!=attr->u.layout) {
1806                         this_->layout_current=attr->u.layout;
1807                         graphics_font_destroy_all(this_->gra);
1808                         navit_set_cursors(this_);
1809                         if (this_->ready == 3)
1810                                 navit_draw(this_);
1811                         attr_updated=1;
1812                 }
1813                 break;
1814         case attr_layout_name:
1815                 l=this_->layouts;
1816                 while (l) {
1817                         lay=l->data;
1818                         if (!strcmp(lay->name,attr->u.str)) {
1819                                 struct attr attr;
1820                                 attr.type=attr_layout;
1821                                 attr.u.layout=lay;
1822                                 return navit_set_attr_do(this_, &attr, init);
1823                         }
1824                         l=g_list_next(l);
1825                 }               
1826                 return 0;
1827         case attr_map_border:
1828                 if (this_->border != attr->u.num) {
1829                         this_->border=attr->u.num;
1830                         attr_updated=1;
1831                 }
1832                 break;
1833         case attr_orientation:
1834                 orient_old=this_->orientation;
1835                 this_->orientation=attr->u.num;
1836                 if (!init) {
1837                         if (this_->orientation != -1) {
1838                                 dir = this_->orientation;
1839                         } else {
1840                                 if (this_->vehicle) {
1841                                         dir = this_->vehicle->dir;
1842                                 }
1843                         }
1844                         transform_set_yaw(this_->trans, dir);
1845                         if (orient_old != this_->orientation) {
1846 #if 0
1847                                 if (this_->ready == 3)
1848                                         navit_draw(this_);
1849 #endif
1850                                 attr_updated=1;
1851                         }
1852                 }
1853                 break;
1854         case attr_osd_configuration:
1855                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1856                 attr_updated=(this_->osd_configuration != attr->u.num);
1857                 this_->osd_configuration=attr->u.num;
1858                 break;
1859         case attr_pitch:
1860                 attr_updated=(this_->pitch != attr->u.num);
1861                 this_->pitch=attr->u.num;
1862                 transform_set_pitch(this_->trans, this_->pitch);
1863                 if (!init && attr_updated && this_->ready == 3)
1864                         navit_draw(this_);
1865                 break;
1866         case attr_projection:
1867                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1868                         navit_projection_set(this_, attr->u.projection, !init);
1869                         attr_updated=1;
1870                 }
1871                 break;
1872         case attr_radius:
1873                 attr_updated=(this_->radius != attr->u.num);
1874                 this_->radius=attr->u.num;
1875                 break;
1876         case attr_recent_dest:
1877                 attr_updated=(this_->recentdest_count != attr->u.num);
1878                 this_->recentdest_count=attr->u.num;
1879                 break;
1880         case attr_speech:
1881                 if(this_->speech && this_->speech != attr->u.speech) {
1882                         attr_updated=1;
1883                         this_->speech = attr->u.speech;
1884                 }
1885                 break;
1886         case attr_timeout:
1887                 attr_updated=(this_->center_timeout != attr->u.num);
1888                 this_->center_timeout = attr->u.num;
1889                 break;
1890         case attr_tracking:
1891                 attr_updated=(this_->tracking_flag != !!attr->u.num);
1892                 this_->tracking_flag=!!attr->u.num;
1893                 break;
1894         case attr_transformation:
1895                 this_->trans=attr->u.transformation;
1896                 break;
1897         case attr_use_mousewheel:
1898                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1899                 this_->use_mousewheel=!!attr->u.num;
1900                 break;
1901         case attr_vehicle:
1902                 l=this_->vehicles;
1903                 while(l) {
1904                         nv=l->data;
1905                         if (nv->vehicle == attr->u.vehicle) {
1906                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1907                                         if (this_->vehicle)
1908                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
1909                                         active.u.num=1;
1910                                         vehicle_set_attr(nv->vehicle, &active);
1911                                         attr_updated=1;
1912                                 }
1913                                 navit_set_vehicle(this_, nv);
1914                         }
1915                         l=g_list_next(l);
1916                 }
1917                 break;
1918         case attr_zoom:
1919                 zoom=transform_get_scale(this_->trans);
1920                 attr_updated=(zoom != attr->u.num);
1921                 transform_set_scale(this_->trans, attr->u.num);
1922                 if (attr_updated && !init) 
1923                         navit_draw(this_);
1924                 break;
1925         case attr_zoom_min:
1926                 attr_updated=(attr->u.num != this_->zoom_min);
1927                 this_->zoom_min=attr->u.num;
1928                 break;
1929         case attr_zoom_max:
1930                 attr_updated=(attr->u.num != this_->zoom_max);
1931                 this_->zoom_max=attr->u.num;
1932                 break;
1933         case attr_message:
1934                 navit_add_message(this_, attr->u.str);
1935                 break;
1936         case attr_follow_cursor:
1937                 attr_updated=(this_->follow_cursor != !!attr->u.num);
1938                 this_->follow_cursor=!!attr->u.num;
1939                 break;
1940         default:
1941                 return 0;
1942         }
1943         if (attr_updated && !init) {
1944                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1945                 if (attr->type == attr_osd_configuration)
1946                         graphics_draw_mode(this_->gra, draw_mode_end);
1947         }
1948         return 1;
1949 }
1950
1951 int
1952 navit_set_attr(struct navit *this_, struct attr *attr)
1953 {
1954         return navit_set_attr_do(this_, attr, 0);
1955 }
1956
1957 int
1958 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1959 {
1960         struct message *msg;
1961         int len,offset;
1962         int ret=1;
1963
1964         switch (type) {
1965         case attr_message:
1966                 msg = navit_get_messages(this_);
1967                 
1968                 if (!msg) {
1969                         return 0;
1970                 }
1971
1972                 len = 0;
1973                 while (msg) {
1974                         len += strlen(msg->text) + 1;
1975                         msg = msg->next;
1976                 }
1977                 attr->u.str = g_malloc(len + 1);
1978                 
1979                 msg = navit_get_messages(this_);
1980                 offset = 0;
1981                 while (msg) {
1982                         g_stpcpy((attr->u.str + offset), msg->text);
1983                         offset += strlen(msg->text);
1984                         attr->u.str[offset] = '\n';
1985                         offset++;
1986
1987                         msg = msg->next;
1988                 }
1989
1990                 attr->u.str[len] = '\0';
1991                 break;
1992         case attr_bookmark_map:
1993                 attr->u.map=bookmarks_get_map(this_->bookmarks);
1994                 break;
1995         case attr_bookmarks:
1996                 attr->u.bookmarks=this_->bookmarks;
1997                 break;
1998         case attr_callback_list:
1999                 attr->u.callback_list=this_->attr_cbl;
2000                 break;
2001         case attr_destination:
2002                 if (! this_->destination_valid)
2003                         return 0;
2004                 attr->u.pcoord=&this_->destination;
2005                 break;
2006         case attr_displaylist:
2007                 attr->u.displaylist=this_->displaylist;
2008                 return (attr->u.displaylist != NULL);
2009         case attr_follow:
2010                 if (!this_->vehicle)
2011                         return 0;
2012                 attr->u.num=this_->vehicle->follow_curr;
2013                 break;
2014         case attr_former_destination_map:
2015                 attr->u.map=this_->former_destination;
2016                 break;
2017         case attr_graphics:
2018                 attr->u.graphics=this_->gra;
2019                 ret=(attr->u.graphics != NULL);
2020                 break;
2021         case attr_gui:
2022                 attr->u.gui=this_->gui;
2023                 ret=(attr->u.gui != NULL);
2024                 break;
2025         case attr_layout:
2026                 if (iter) {
2027                         if (iter->u.list) {
2028                                 iter->u.list=g_list_next(iter->u.list);
2029                         } else { 
2030                                 iter->u.list=this_->layouts;
2031                         }
2032                         if (!iter->u.list)
2033                                 return 0;
2034                         attr->u.layout=(struct layout *)iter->u.list->data;
2035                 } else {
2036                         attr->u.layout=this_->layout_current;
2037                 }
2038                 break;
2039         case attr_map:
2040                 if (iter && this_->mapsets) {
2041                         if (!iter->u.mapset_handle) {
2042                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2043                         }
2044                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2045                         if(!attr->u.map) {
2046                                 mapset_close(iter->u.mapset_handle);
2047                                 return 0;
2048                         }
2049                 } else {
2050                         return 0;
2051                 }
2052                 break;
2053         case attr_mapset:
2054                 attr->u.mapset=this_->mapsets->data;
2055                 ret=(attr->u.mapset != NULL);
2056                 break;
2057         case attr_navigation:
2058                 attr->u.navigation=this_->navigation;
2059                 break;
2060         case attr_orientation:
2061                 attr->u.num=this_->orientation;
2062                 break;
2063         case attr_osd_configuration:
2064                 attr->u.num=this_->osd_configuration;
2065                 break;
2066         case attr_pitch:
2067                 attr->u.num=transform_get_pitch(this_->trans);
2068                 break;
2069         case attr_projection:
2070                 if(this_->trans) {
2071                         attr->u.num=transform_get_projection(this_->trans);
2072                 } else {
2073                         return 0;
2074                 }
2075                 break;
2076         case attr_route:
2077                 attr->u.route=this_->route;
2078                 break;
2079         case attr_speech:
2080                 attr->u.speech=this_->speech;
2081                 break;
2082         case attr_tracking:
2083                 attr->u.num=this_->tracking_flag;
2084                 break;
2085         case attr_trackingo:
2086                 attr->u.tracking=this_->tracking;
2087                 break;
2088         case attr_transformation:
2089                 attr->u.transformation=this_->trans;
2090                 break;
2091         case attr_vehicle:
2092                 if(iter) {
2093                         if(iter->u.list) {
2094                                 iter->u.list=g_list_next(iter->u.list);
2095                         } else { 
2096                                 iter->u.list=this_->vehicles;
2097                         }
2098                         if(!iter->u.list)
2099                                 return 0;
2100                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2101                 } else {
2102                         if(this_->vehicle) {
2103                                 attr->u.vehicle=this_->vehicle->vehicle;
2104                         } else {
2105                                 return 0;
2106                         }
2107                 }
2108                 break;
2109         case attr_vehicleprofile:
2110                 attr->u.vehicleprofile=this_->vehicleprofile;
2111                 break;
2112         case attr_zoom:
2113                 attr->u.num=transform_get_scale(this_->trans);
2114                 break;
2115         case attr_autozoom_active:
2116                 attr->u.num=this_->autozoom_active;
2117                 break;
2118         case attr_follow_cursor:
2119                 attr->u.num=this_->follow_cursor;
2120                 break;
2121         default:
2122                 return 0;
2123         }
2124         attr->type=type;
2125         return ret;
2126 }
2127
2128 static int
2129 navit_add_log(struct navit *this_, struct log *log)
2130 {
2131         struct attr type_attr;
2132         if (!log_get_attr(log, attr_type, &type_attr, NULL))
2133                 return 0;
2134         if (!strcmp(type_attr.u.str, "textfile_debug")) {
2135                 char *header = "type=track_tracked\n";
2136                 if (this_->textfile_debug_log)
2137                         return 0;
2138                 log_set_header(log, header, strlen(header));
2139                 this_->textfile_debug_log=log;
2140                 return 1;
2141         }
2142         return 0;
2143 }
2144
2145 int
2146 navit_add_attr(struct navit *this_, struct attr *attr)
2147 {
2148         int ret=1;
2149         switch (attr->type) {
2150         case attr_callback:
2151                 navit_add_callback(this_, attr->u.callback);
2152                 break;
2153         case attr_log:
2154                 ret=navit_add_log(this_, attr->u.log);
2155                 break;
2156         case attr_gui:
2157                 ret=navit_set_gui(this_, attr->u.gui);
2158                 break;
2159         case attr_graphics:
2160                 ret=navit_set_graphics(this_, attr->u.graphics);
2161                 break;
2162         case attr_layout:
2163                 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2164                 if(!this_->layout_current) 
2165                         this_->layout_current=attr->u.layout;
2166                 break;
2167         case attr_route:
2168                 this_->route=attr->u.route;
2169                 break;
2170         case attr_mapset:
2171                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2172                 break;
2173         case attr_navigation:
2174                 this_->navigation=attr->u.navigation;
2175                 break;
2176         case attr_recent_dest:
2177                 this_->recentdest_count = attr->u.num;
2178                 break;
2179         case attr_speech:
2180                 this_->speech=attr->u.speech;
2181                 break;
2182         case attr_tracking:
2183                 this_->tracking=attr->u.tracking;
2184                 break;
2185         case attr_vehicle:
2186                 ret=navit_add_vehicle(this_, attr->u.vehicle);
2187                 break;
2188         case attr_vehicleprofile:
2189                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2190                 break;
2191         case attr_autozoom_min:
2192                 this_->autozoom_min = attr->u.num;
2193                 break;
2194         default:
2195                 return 0;
2196         }
2197         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2198         return ret;
2199 }
2200
2201 int
2202 navit_remove_attr(struct navit *this_, struct attr *attr)
2203 {
2204         int ret=1;
2205         switch (attr->type) {
2206         case attr_callback:
2207                 navit_remove_callback(this_, attr->u.callback);
2208                 break;
2209         default:
2210                 return 0;
2211         }
2212         return ret;
2213 }
2214
2215 struct attr_iter *
2216 navit_attr_iter_new(void)
2217 {
2218         return g_new0(struct attr_iter, 1);
2219 }
2220
2221 void
2222 navit_attr_iter_destroy(struct attr_iter *iter)
2223 {
2224         g_free(iter);
2225 }
2226
2227 void
2228 navit_add_callback(struct navit *this_, struct callback *cb)
2229 {
2230         callback_list_add(this_->attr_cbl, cb);
2231 }
2232
2233 void
2234 navit_remove_callback(struct navit *this_, struct callback *cb)
2235 {
2236         callback_list_remove(this_->attr_cbl, cb);
2237 }
2238
2239 /**
2240  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2241  *
2242  * @param navit The navit instance
2243  * @returns nothing
2244  */
2245
2246 static void
2247 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2248 {
2249         struct point cursor_pnt;
2250         enum projection pro;
2251
2252         if (this_->blocked)
2253                 return;
2254         if (pnt)
2255                 cursor_pnt=*pnt;
2256         else {
2257                 pro=transform_get_projection(this_->trans_cursor);
2258                 if (!pro)
2259                         return;
2260                 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2261         }
2262         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2263 #if 0   
2264         if (pnt)
2265                 pnt2=*pnt;
2266         else {
2267                 pro=transform_get_projection(this_->trans);
2268                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2269         }
2270 #if 1
2271         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2272 #else
2273         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2274 #endif
2275 #endif
2276 }
2277
2278 static void
2279 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2280 {
2281         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2282         struct pcoord cursor_pc;
2283         struct point cursor_pnt, *pnt=&cursor_pnt;
2284         struct tracking *tracking=NULL;
2285         struct pcoord pc[16];
2286         enum projection pro=transform_get_projection(this_->trans_cursor);
2287         int count;
2288         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2289         void *attr_object;
2290         char *destination_file;
2291
2292         profile(0,NULL);
2293         if (this_->ready != 3) {
2294                 profile(0,"return 1\n");
2295                 return;
2296         }
2297         navit_layout_switch(this_);
2298         if (this_->vehicle == nv && this_->tracking_flag)
2299                 tracking=this_->tracking;
2300         if (tracking) {
2301                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2302                 attr_object=tracking;
2303                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2304         } else {
2305                 attr_object=nv->vehicle;
2306                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2307         }
2308         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2309                 if (!attr_valid.u.num != attr_position_valid_invalid)
2310                         return;
2311         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2312             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2313             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2314                 profile(0,"return 2\n");
2315                 return;
2316         }
2317         nv->dir=*attr_dir.u.numd;
2318         nv->speed=*attr_speed.u.numd;
2319         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2320         if (nv != this_->vehicle) {
2321                 navit_vehicle_draw(this_, nv, NULL);
2322                 profile(0,"return 3\n");
2323                 return;
2324         }
2325         cursor_pc.x = nv->coord.x;
2326         cursor_pc.y = nv->coord.y;
2327         cursor_pc.pro = pro;
2328         if (this_->route) {
2329                 if (tracking)
2330                         route_set_position_from_tracking(this_->route, tracking, pro);
2331                 else
2332                         route_set_position(this_->route, &cursor_pc);
2333         }
2334         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2335         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2336         if (this_->gui && nv->speed > 2)
2337                 navit_disable_suspend();
2338
2339         transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2340         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2341                 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2342                 navit_set_center_cursor_draw(this_);
2343         else
2344                 navit_vehicle_draw(this_, nv, pnt);
2345
2346         if (nv->follow_curr > 1)
2347                 nv->follow_curr--;
2348         else
2349                 nv->follow_curr=nv->follow;
2350         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2351
2352         /* Finally, if we reached our destination, stop navigation. */
2353         if (this_->route) {
2354                 switch(route_destination_reached(this_->route)) {
2355                 case 1:
2356                         route_remove_waypoint(this_->route);
2357                         count=route_get_destinations(this_->route, pc, 16);
2358                         destination_file = bookmarks_get_destination_file(TRUE);
2359                         bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2360                         break;  
2361                 case 2:
2362                         navit_set_destination(this_, NULL, NULL, 0);
2363                         break;
2364                 }
2365         }
2366         profile(0,"return 5\n");
2367 }
2368
2369 /**
2370  * Set the position of the vehicle
2371  *
2372  * @param navit The navit instance
2373  * @param c The coordinate to set as position
2374  * @returns nothing
2375  */
2376
2377 void
2378 navit_set_position(struct navit *this_, struct pcoord *c)
2379 {
2380         if (this_->route) {
2381                 route_set_position(this_->route, c);
2382                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2383         }
2384         if (this_->ready == 3)
2385                 navit_draw(this_);
2386 }
2387
2388 static int
2389 navit_set_vehicleprofile(struct navit *this_, char *name)
2390 {
2391         struct attr attr;
2392         GList *l;
2393         l=this_->vehicleprofiles;
2394         while (l) {
2395                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2396                         if (!strcmp(attr.u.str, name)) {
2397                                 this_->vehicleprofile=l->data;
2398                                 if (this_->route)
2399                                         route_set_profile(this_->route, this_->vehicleprofile);
2400                                 return 1;
2401                         }
2402                 }
2403                 l=g_list_next(l);
2404         }
2405         return 0;
2406 }
2407
2408 static void
2409 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2410 {
2411         struct attr attr;
2412         this_->vehicle=nv;
2413         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2414                 if (navit_set_vehicleprofile(this_, attr.u.str))
2415                         return;
2416         }
2417         if (!navit_set_vehicleprofile(this_,"car")) {
2418                 /* We do not have a fallback "car" profile
2419                 * so lets set any profile */
2420                 GList *l;
2421                 l=this_->vehicleprofiles;
2422                 if (l) {
2423                     this_->vehicleprofile=l->data;
2424                     if (this_->route)
2425                         route_set_profile(this_->route, this_->vehicleprofile);
2426                 }
2427         }
2428 }
2429
2430 /**
2431  * Register a new vehicle
2432  *
2433  * @param navit The navit instance
2434  * @param v The vehicle instance
2435  * @returns 1 for success
2436  */
2437 static int
2438 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2439 {
2440         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2441         struct attr follow, active, animate;
2442         nv->vehicle=v;
2443         nv->follow=0;
2444         nv->last.x = 0;
2445         nv->last.y = 0;
2446         nv->animate_cursor=0;
2447         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2448                 nv->follow=follow.u.num;
2449         nv->follow_curr=nv->follow;
2450         this_->vehicles=g_list_append(this_->vehicles, nv);
2451         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2452                 navit_set_vehicle(this_, nv);
2453         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2454                 nv->animate_cursor=animate.u.num;
2455         nv->callback.type=attr_callback;
2456         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2457         vehicle_add_attr(nv->vehicle, &nv->callback);
2458         vehicle_set_attr(nv->vehicle, &this_->self);
2459         return 1;
2460 }
2461
2462
2463
2464
2465 struct gui *
2466 navit_get_gui(struct navit *this_)
2467 {
2468         return this_->gui;
2469 }
2470
2471 struct transformation *
2472 navit_get_trans(struct navit *this_)
2473 {
2474         return this_->trans;
2475 }
2476
2477 struct route *
2478 navit_get_route(struct navit *this_)
2479 {
2480         return this_->route;
2481 }
2482
2483 struct navigation *
2484 navit_get_navigation(struct navit *this_)
2485 {
2486         return this_->navigation;
2487 }
2488
2489 struct displaylist *
2490 navit_get_displaylist(struct navit *this_)
2491 {
2492         return this_->displaylist;
2493 }
2494
2495 void
2496 navit_layout_switch(struct navit *n) 
2497 {
2498
2499     int currTs=0;
2500     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2501     double trise,tset,trise_actual;
2502     struct layout *l;
2503     int year, month, day;
2504     
2505     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2506         return; //No layout - nothing to switch
2507     }
2508     if (!n->vehicle)
2509         return;
2510     l=layout_attr.u.layout;
2511     
2512     if (l->dayname || l->nightname) {
2513         //Ok, we know that we have profile to switch
2514         
2515         //Check that we aren't calculating too fast
2516         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2517                 currTs=iso8601_to_secs(iso8601_attr.u.str);
2518                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2519         }
2520         if (currTs-(n->prevTs)<60) {
2521             //We've have to wait a little
2522             return;
2523         }
2524         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2525                 return;
2526         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2527                 return; //No valid fix yet
2528         }
2529         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2530                 //No position - no sun
2531                 return;
2532         }
2533         
2534         //We calculate sunrise anyway, cause it is needed both for day and for night
2535         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2536                 //near the pole sun never rises/sets, so we should never switch profiles
2537                 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2538                 n->prevTs=currTs;
2539                 return;
2540             }
2541         
2542         trise_actual=trise;
2543         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2544         if (l->dayname) {
2545         
2546             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
2547                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2548                 //The sun is rising now!
2549                 if (strcmp(l->name,l->dayname)) {
2550                     navit_set_layout_by_name(n,l->dayname);
2551                 }
2552             }
2553         }
2554         if (l->nightname) {
2555             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2556                 //near the pole sun never rises/sets, so we should never switch profiles
2557                 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2558                 n->prevTs=currTs;
2559                 return;
2560             }
2561             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2562             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2563                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
2564                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2565                 //Time to sleep
2566                 if (strcmp(l->name,l->nightname)) {
2567                     navit_set_layout_by_name(n,l->nightname);
2568                 }
2569             }   
2570         }
2571         
2572         n->prevTs=currTs;
2573     }
2574 }
2575
2576 int 
2577 navit_set_vehicle_by_name(struct navit *n,const char *name) 
2578 {
2579     struct vehicle *v;
2580     struct attr_iter *iter;
2581     struct attr vehicle_attr, name_attr;
2582
2583         iter=navit_attr_iter_new();
2584
2585     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2586                 v=vehicle_attr.u.vehicle;
2587                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2588                 if (name_attr.type==attr_name) {
2589                         if (!strcmp(name,name_attr.u.str)) {
2590                                 navit_set_attr(n,&vehicle_attr);                                
2591                                 navit_attr_iter_destroy(iter);
2592                                 return 1;
2593                         }
2594                 }
2595         }
2596     navit_attr_iter_destroy(iter);
2597     return 0;
2598 }
2599
2600 int 
2601 navit_set_layout_by_name(struct navit *n,const char *name) 
2602 {
2603     struct layout *l;
2604     struct attr_iter iter;
2605     struct attr layout_attr;
2606
2607     iter.u.list=0x00;
2608
2609     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2610         return 0; //No layouts - nothing to do
2611     }
2612     if (iter.u.list==NULL) {
2613         return 0;
2614     }
2615     
2616     iter.u.list=g_list_first(iter.u.list);
2617     
2618     while(iter.u.list) {
2619         l=(struct layout*)iter.u.list->data;
2620         if (!strcmp(name,l->name)) {
2621             layout_attr.u.layout=l;
2622             layout_attr.type=attr_layout;
2623             navit_set_attr(n,&layout_attr);
2624             iter.u.list=g_list_first(iter.u.list);
2625             return 1;
2626         }
2627         iter.u.list=g_list_next(iter.u.list);
2628     }
2629
2630     iter.u.list=g_list_first(iter.u.list);
2631     return 0;
2632 }
2633
2634 void
2635 navit_disable_suspend() {
2636         gui_disable_suspend(global_navit->gui);
2637         callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2638 }
2639
2640 int
2641 navit_block(struct navit *this_, int block)
2642 {
2643         if (block > 0) {
2644                 this_->blocked |= 1;
2645                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2646                         this_->blocked |= 2;
2647                 return 0;
2648         }
2649         if ((this_->blocked & 2) || block < 0) {
2650                 this_->blocked=0;
2651                 navit_draw(this_);
2652                 return 1;
2653         }
2654         this_->blocked=0;
2655         return 0;
2656 }
2657
2658 void
2659 navit_destroy(struct navit *this_)
2660 {
2661         callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2662
2663         /* TODO: destroy objects contained in this_ */
2664         if (this_->vehicle)
2665                 vehicle_destroy(this_->vehicle->vehicle);
2666         if (this_->bookmarks) {
2667                 char *center_file = bookmarks_get_center_file(TRUE);
2668                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2669                 g_free(center_file);
2670                 bookmarks_destroy(this_->bookmarks);
2671         }
2672         callback_destroy(this_->nav_speech_cb);
2673         callback_destroy(this_->roadbook_callback);
2674         callback_destroy(this_->popup_callback);
2675         callback_destroy(this_->motion_timeout_callback);
2676         if(this_->gra)
2677           graphics_remove_callback(this_->gra, this_->resize_callback);
2678         callback_destroy(this_->resize_callback);
2679         if(this_->gra)
2680           graphics_remove_callback(this_->gra, this_->button_callback);
2681         callback_destroy(this_->button_callback);
2682         if(this_->gra)
2683           graphics_remove_callback(this_->gra, this_->motion_callback);
2684         callback_destroy(this_->motion_callback);
2685         if(this_->gra)
2686           graphics_remove_callback(this_->gra, this_->predraw_callback);
2687         callback_destroy(this_->predraw_callback);
2688         route_destroy(this_->route);
2689         g_free(this_);
2690 }
2691
2692 /** @} */