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