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