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