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