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