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