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