Fix:Core:Canclel displaylist before destroying navit, make zoom_to_rect use transform...
[profile/ivi/navit.git] / navit / navit / navit.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2009 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #define _USE_MATH_DEFINES 1
21 #include "config.h"
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include <stdio.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <glib.h>
32 #include <math.h>
33 #include <time.h>
34 #include "debug.h"
35 #include "navit.h"
36 #include "callback.h"
37 #include "gui.h"
38 #include "item.h"
39 #include "projection.h"
40 #include "map.h"
41 #include "mapset.h"
42 #include "main.h"
43 #include "coord.h"
44 #include "point.h"
45 #include "transform.h"
46 #include "param.h"
47 #include "menu.h"
48 #include "graphics.h"
49 #include "popup.h"
50 #include "data_window.h"
51 #include "route.h"
52 #include "navigation.h"
53 #include "speech.h"
54 #include "track.h"
55 #include "vehicle.h"
56 #include "layout.h"
57 #include "log.h"
58 #include "attr.h"
59 #include "event.h"
60 #include "file.h"
61 #include "profile.h"
62 #include "command.h"
63 #include "navit_nls.h"
64 #include "map.h"
65 #include "util.h"
66 #include "messages.h"
67 #include "vehicleprofile.h"
68 #include "sunriset.h"
69 #include "bookmarks.h"
70 #include "xmlconfig.h"
71 #ifdef HAVE_API_WIN32_BASE
72 #include <windows.h>
73 #include "util.h"
74 #endif
75 #ifdef HAVE_API_WIN32_CE
76 #include "libc.h"
77 #endif
78
79 /* define string for bookmark handling */
80 #define TEXTFILE_COMMENT_NAVI_STOPPED "# navigation stopped\n"
81
82 /**
83  * @defgroup navit the navit core instance. navit is the object containing nearly everything: A set of maps, one or more vehicle, a graphics object for rendering the map, a gui object for displaying the user interface, a route object, a navigation object and so on. Be warned that it is theoretically possible to have more than one navit object
84  * @{
85  */
86
87 //! The navit_vehicule
88 struct navit_vehicle {
89         int follow;
90         /*! Limit of the follow counter. See navit_add_vehicle */
91         int follow_curr;
92         /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
93         struct coord coord;
94         int dir;
95         int speed;
96         struct coord last; /*< Position of the last update of this vehicle */
97         struct vehicle *vehicle;
98         struct attr callback;
99         int animate_cursor;
100 };
101
102 struct navit {
103         struct object_func *func;
104         int refcount;
105         struct attr **attrs;
106         struct attr self;
107         GList *mapsets;
108         GList *layouts;
109         struct gui *gui;
110         struct layout *layout_current;
111         struct graphics *gra;
112         struct action *action;
113         struct transformation *trans, *trans_cursor;
114         struct compass *compass;
115         struct route *route;
116         struct navigation *navigation;
117         struct speech *speech;
118         struct tracking *tracking;
119         int ready;
120         struct window *win;
121         struct displaylist *displaylist;
122         int tracking_flag;
123         int orientation;
124         int recentdest_count;
125         int osd_configuration;
126         GList *vehicles;
127         GList *windows_items;
128         struct navit_vehicle *vehicle;
129         struct callback_list *attr_cbl;
130         struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
131         struct datawindow *roadbook_window;
132         struct map *former_destination;
133         struct point pressed, last, current;
134         int button_pressed,moved,popped,zoomed;
135         int center_timeout;
136         int autozoom_secs;
137         int autozoom_min;
138         int autozoom_active;
139         struct event_timeout *button_timeout, *motion_timeout;
140         struct callback *motion_timeout_callback;
141         int ignore_button;
142         int ignore_graphics_events;
143         struct log *textfile_debug_log;
144         struct pcoord destination;
145         int destination_valid;
146         int blocked;
147         int w,h;
148         int drag_bitmap;
149         int use_mousewheel;
150         struct messagelist *messages;
151         struct callback *resize_callback,*button_callback,*motion_callback,*predraw_callback;
152         struct vehicleprofile *vehicleprofile;
153         GList *vehicleprofiles;
154         int pitch;
155         int follow_cursor;
156         int prevTs;
157         int graphics_flags;
158         int zoom_min, zoom_max;
159         int radius;
160         struct bookmarks *bookmarks;
161         int flags;
162                  /* 1=No graphics ok */
163                  /* 2=No gui ok */
164         int border;
165         int imperial;
166         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         dbg(0,"return %p\n",this_);
1419         
1420         return this_;
1421 }
1422
1423 static int
1424 navit_set_gui(struct navit *this_, struct gui *gui)
1425 {
1426         if (this_->gui)
1427                 return 0;
1428         this_->gui=gui;
1429         if (gui_has_main_loop(this_->gui)) {
1430                 if (! main_loop_gui) {
1431                         main_loop_gui=this_->gui;
1432                 } else {
1433                         dbg(0,"gui with main loop already active, ignoring this instance");
1434                         return 0;
1435                 }
1436         }
1437         return 1;
1438 }
1439
1440 void 
1441 navit_add_message(struct navit *this_, char *message)
1442 {
1443         message_new(this_->messages, message);
1444 }
1445
1446 struct message
1447 *navit_get_messages(struct navit *this_)
1448 {
1449         return message_get(this_->messages);
1450 }
1451
1452 static int
1453 navit_set_graphics(struct navit *this_, struct graphics *gra)
1454 {
1455         if (this_->gra)
1456                 return 0;
1457         this_->gra=gra;
1458         this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1459         graphics_add_callback(gra, this_->resize_callback);
1460         this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1461         graphics_add_callback(gra, this_->button_callback);
1462         this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1463         graphics_add_callback(gra, this_->motion_callback);
1464         this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1465         graphics_add_callback(gra, this_->predraw_callback);
1466         return 1;
1467 }
1468
1469 struct graphics *
1470 navit_get_graphics(struct navit *this_)
1471 {
1472         return this_->gra;
1473 }
1474
1475 struct vehicleprofile *
1476 navit_get_vehicleprofile(struct navit *this_)
1477 {
1478         return this_->vehicleprofile;
1479 }
1480
1481 GList *
1482 navit_get_vehicleprofiles(struct navit *this_)
1483 {
1484         return this_->vehicleprofiles;
1485 }
1486
1487 static void
1488 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1489 {
1490         struct coord_geo g;
1491         struct coord *c;
1492
1493         c=transform_center(this_->trans);
1494         transform_to_geo(transform_get_projection(this_->trans), c, &g);
1495         transform_set_projection(this_->trans, pro);
1496         transform_from_geo(pro, &g, c);
1497         if (draw)
1498                 navit_draw(this_);
1499 }
1500
1501 static void
1502 navit_mark_navigation_stopped(char *former_destination_file){
1503         FILE *f;
1504         f=fopen(former_destination_file, "a");
1505         if (f) {
1506                 fprintf(f,"%s", TEXTFILE_COMMENT_NAVI_STOPPED);
1507                 fclose(f);
1508         }else{
1509                 dbg(0, "Error setting mark in destination file %s: %s\n", former_destination_file, strerror(errno));
1510         }
1511 }
1512
1513 /**
1514  * Start the route computing to a given set of coordinates
1515  *
1516  * @param navit The navit instance
1517  * @param c The coordinate to start routing to
1518  * @param description A label which allows the user to later identify this destination in the former destinations selection
1519  * @returns nothing
1520  */
1521 void
1522 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1523 {
1524         char *destination_file;
1525         destination_file = bookmarks_get_destination_file(TRUE);
1526         if (c) {
1527                 this_->destination=*c;
1528                 this_->destination_valid=1;
1529
1530                 dbg(1, "c=(%i,%i)\n", c->x,c->y);
1531                 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_destination, description, this_->recentdest_count);
1532         } else {
1533                 this_->destination_valid=0;
1534                 navit_mark_navigation_stopped(destination_file);
1535         }
1536         g_free(destination_file);
1537         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1538         if (this_->route) {
1539                 route_set_destination(this_->route, c, async);
1540
1541                 if (this_->ready == 3)
1542                         navit_draw(this_);
1543         }
1544 }
1545
1546 /**
1547  * Start the route computing to a given set of coordinates including waypoints
1548  *
1549  * @param navit The navit instance
1550  * @param c The coordinate to start routing to
1551  * @param description A label which allows the user to later identify this destination in the former destinations selection
1552  * @returns nothing
1553  */
1554 void
1555 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1556 {
1557         char *destination_file;
1558         if (c && count) {
1559                 this_->destination=c[count-1];
1560                 this_->destination_valid=1;
1561
1562                 destination_file = bookmarks_get_destination_file(TRUE);
1563                 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_itinerary, description, this_->recentdest_count);
1564                 g_free(destination_file);
1565         } else
1566                 this_->destination_valid=0;
1567         callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1568         if (this_->route) {
1569                 route_set_destinations(this_->route, c, count, async);
1570
1571                 if (this_->ready == 3)
1572                         navit_draw(this_);
1573         }
1574 }
1575
1576 /**
1577  * @brief Checks if a route is calculated
1578  *
1579  * This function checks if a route is calculated.
1580  *
1581  * @param this_ The navit struct whose route should be checked.
1582  * @return True if the route is set, false otherwise.
1583  */
1584 int
1585 navit_check_route(struct navit *this_)
1586 {
1587         if (this_->route) {
1588                 return route_get_path_set(this_->route);
1589         }
1590
1591         return 0;
1592 }
1593
1594 static int
1595 navit_former_destinations_active(struct navit *this_)
1596 {
1597         char *destination_file_name = bookmarks_get_destination_file(FALSE);
1598         FILE *destination_file;
1599         int active=0;
1600         char lastline[100];
1601         destination_file=fopen(destination_file_name,"r");
1602         if (destination_file) {
1603                 while(fgets(lastline, sizeof(lastline), destination_file));
1604                 fclose(destination_file);
1605                 if ((lastline != NULL) && (strcmp(lastline, TEXTFILE_COMMENT_NAVI_STOPPED))){
1606                         active=1;
1607                 }
1608         }
1609         g_free(destination_file_name);
1610         return active;
1611 }
1612
1613 struct map* read_former_destinations_from_file(){
1614         struct attr type, data, flags, *attrs[4];
1615         char *destination_file = bookmarks_get_destination_file(FALSE);
1616         struct map *m;
1617
1618         type.type=attr_type;
1619         type.u.str="textfile";
1620
1621         data.type=attr_data;
1622         data.u.str=destination_file;
1623
1624         flags.type=attr_flags;
1625         flags.u.num=1;
1626
1627         attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1628
1629         m=map_new(NULL, attrs);
1630         g_free(destination_file);
1631         return m;
1632 }
1633
1634 static void
1635 navit_add_former_destinations_from_file(struct navit *this_)
1636 {
1637         struct item *item;
1638         int i,valid=0,count=0;
1639         struct coord c[16];
1640         struct pcoord pc[16];
1641         struct map_rect *mr;
1642
1643         this_->former_destination=read_former_destinations_from_file();
1644         if (!this_->route || !navit_former_destinations_active(this_))
1645                 return; 
1646         mr=map_rect_new(this_->former_destination, NULL);
1647         while ((item=map_rect_get_item(mr))) {
1648                 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16))) 
1649                         valid=1;
1650         }
1651         map_rect_destroy(mr);
1652         if (valid && count > 0) {
1653                 for (i = 0 ; i < count ; i++) {
1654                         pc[i].pro=map_projection(this_->former_destination);
1655                         pc[i].x=c[i].x;
1656                         pc[i].y=c[i].y;
1657                 }
1658                 if (count == 1)
1659                         route_set_destination(this_->route, &pc[0], 1);
1660                 else
1661                         route_set_destinations(this_->route, pc, count, 1);
1662                 this_->destination=pc[count-1];
1663                 this_->destination_valid=1;
1664         }
1665 }
1666
1667
1668 void
1669 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1670 {
1671         va_list ap;
1672         char *str1,*str2;
1673         va_start(ap, fmt);
1674         if (this_->textfile_debug_log && this_->vehicle) {
1675                 str1=g_strdup_vprintf(fmt, ap);
1676                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1677                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1678                 g_free(str2);
1679                 g_free(str1);
1680         }
1681         va_end(ap);
1682 }
1683
1684 void
1685 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1686 {
1687         va_list ap;
1688         char *str1,*str2;
1689         va_start(ap, fmt);
1690         if (this_->textfile_debug_log && this_->vehicle) {
1691                 str1=g_strdup_vprintf(fmt, ap);
1692                 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1693                 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1694                 g_free(str2);
1695                 g_free(str1);
1696         }
1697         va_end(ap);
1698 }
1699
1700 void
1701 navit_say(struct navit *this_, char *text)
1702 {
1703         struct attr attr;
1704         if(this_->speech) {
1705                 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1706                         attr.u.num = 1;
1707                 dbg(1, "this_.speech->active %i\n", attr.u.num);
1708                 if(attr.u.num)
1709                         speech_say(this_->speech, text);
1710         }
1711 }
1712
1713 /**
1714  * @brief Toggles the navigation announcer for navit
1715  * @param this_ The navit object
1716  */
1717 static void
1718 navit_cmd_announcer_toggle(struct navit *this_)
1719 {
1720     struct attr attr, speechattr;
1721
1722     // search for the speech attribute
1723     if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1724         return;
1725     // find out if the corresponding attribute attr_active has been set
1726     if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1727         // flip it then...
1728         attr.u.num = !attr.u.num;
1729     } else {
1730         // otherwise disable it because voice is enabled by default
1731         attr.type = attr_active;
1732         attr.u.num = 0;
1733     }
1734
1735     // apply the new state
1736     if(!speech_set_attr(speechattr.u.speech, &attr))
1737         return;
1738
1739     // announce that the speech attribute has changed
1740     callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1741 }
1742
1743 void
1744 navit_speak(struct navit *this_)
1745 {
1746         struct navigation *nav=this_->navigation;
1747         struct map *map=NULL;
1748         struct map_rect *mr=NULL;
1749         struct item *item;
1750         struct attr attr;
1751
1752     if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1753         attr.u.num = 1;
1754     dbg(1, "this_.speech->active %i\n", attr.u.num);
1755     if(!attr.u.num)
1756         return;
1757
1758         if (nav)
1759                 map=navigation_get_map(nav);
1760         if (map)
1761                 mr=map_rect_new(map, NULL);
1762         if (mr) {
1763                 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1764                 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1765                         speech_say(this_->speech, attr.u.str);
1766                         navit_add_message(this_, attr.u.str);
1767                         navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1768                 }
1769                 map_rect_destroy(mr);
1770         }
1771 }
1772
1773 static void
1774 navit_window_roadbook_update(struct navit *this_)
1775 {
1776         struct navigation *nav=this_->navigation;
1777         struct map *map=NULL;
1778         struct map_rect *mr=NULL;
1779         struct item *item;
1780         struct attr attr;
1781         struct param_list param[5];
1782         int secs;
1783
1784         dbg(1,"enter\n");
1785         datawindow_mode(this_->roadbook_window, 1);
1786         if (nav)
1787                 map=navigation_get_map(nav);
1788         if (map)
1789                 mr=map_rect_new(map, NULL);
1790         dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1791         if (mr) {
1792                 dbg(0,"while loop\n");
1793                 while ((item=map_rect_get_item(mr))) {
1794                         dbg(0,"item=%p\n", item);
1795                         attr.u.str=NULL;
1796                         if (item->type != type_nav_position) {
1797                                 item_attr_get(item, attr_navigation_long, &attr);
1798                                 if (attr.u.str == NULL) {
1799                                         continue;
1800                                 }
1801                                 dbg(2, "Command='%s'\n", attr.u.str);
1802                                 param[0].value=g_strdup(attr.u.str);
1803                         } else
1804                                 param[0].value=_("Position");
1805                         param[0].name=_("Command");
1806
1807                         item_attr_get(item, attr_length, &attr);
1808                         dbg(2, "Length=%d\n", attr.u.num);
1809                         param[1].name=_("Length");
1810
1811                         if ( attr.u.num >= 2000 )
1812                         {
1813                                 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1814                         }
1815                         else
1816                         {
1817                                 param[1].value=g_strdup_printf("%7ld %s",attr.u.num, _("m"));
1818                         }
1819
1820                         item_attr_get(item, attr_time, &attr);
1821                         dbg(2, "Time=%d\n", attr.u.num);
1822                         secs=attr.u.num/10;
1823                         param[2].name=_("Time");
1824                         if ( secs >= 3600 )
1825                         {
1826                                 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1827                         }
1828                         else
1829                         {
1830                                 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1831                         }
1832
1833                         item_attr_get(item, attr_destination_length, &attr);
1834                         dbg(2, "Destlength=%d\n", attr.u.num);
1835                         param[3].name=_("Destination Length");
1836                         if ( attr.u.num >= 2000 )
1837                         {
1838                                 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1839                         }
1840                         else
1841                         {
1842                                 param[3].value=g_strdup_printf("%ld %s",attr.u.num, _("m"));
1843                         }
1844
1845                         item_attr_get(item, attr_destination_time, &attr);
1846                         dbg(2, "Desttime=%d\n", attr.u.num);
1847                         secs=attr.u.num/10;
1848                         param[4].name=_("Destination Time");
1849                         if ( secs >= 3600 )
1850                         {
1851                                 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1852                         }
1853                         else
1854                         {
1855                                 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1856                         }
1857                         datawindow_add(this_->roadbook_window, param, 5);
1858                 }
1859                 map_rect_destroy(mr);
1860         }
1861         datawindow_mode(this_->roadbook_window, 0);
1862 }
1863
1864 void
1865 navit_window_roadbook_destroy(struct navit *this_)
1866 {
1867         dbg(0, "enter\n");
1868         navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1869         callback_destroy(this_->roadbook_callback);
1870         this_->roadbook_window=NULL;
1871         this_->roadbook_callback=NULL;
1872 }
1873 void
1874 navit_window_roadbook_new(struct navit *this_)
1875 {
1876         if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1877                 return;
1878         }
1879
1880         this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1881         navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1882         this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1883         navit_window_roadbook_update(this_);
1884 }
1885
1886 void
1887 navit_init(struct navit *this_)
1888 {
1889         struct mapset *ms;
1890         struct map *map;
1891         int callback;
1892         char *center_file;
1893
1894         dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1895
1896         if (!this_->gui && !(this_->flags & 2)) {
1897                 dbg(0,"no gui\n");
1898                 navit_destroy(this_);
1899                 return;
1900         }
1901         if (!this_->gra && !(this_->flags & 1)) {
1902                 dbg(0,"no graphics\n");
1903                 navit_destroy(this_);
1904                 return;
1905         }
1906         dbg(2,"Connecting gui to graphics\n");
1907         if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1908                 struct attr attr_type_gui, attr_type_graphics;
1909                 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1910                 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1911                 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1912                 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1913                 dbg(0," for explanations and solutions\n");
1914
1915                 navit_destroy(this_);
1916                 return;
1917         }
1918         if (this_->speech && this_->navigation) {
1919                 struct attr speech;
1920                 speech.type=attr_speech;
1921                 speech.u.speech=this_->speech;
1922                 navigation_set_attr(this_->navigation, &speech);
1923         }
1924         dbg(2,"Initializing graphics\n");
1925         dbg(2,"Setting Vehicle\n");
1926         navit_set_vehicle(this_, this_->vehicle);
1927         dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1928         if (this_->mapsets) {
1929                 struct mapset_handle *msh;
1930                 ms=this_->mapsets->data;
1931                 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1932                 msh=mapset_open(ms);
1933                 while (msh && (map=mapset_next(msh, 0))) {
1934                         //pass new callback instance for each map in the mapset to make map callback list destruction work correctly
1935                         struct callback *pcb = callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1936                         map_add_callback(map, pcb);
1937                 }
1938                 mapset_close(msh);
1939                 
1940                 if (this_->route) {
1941                         if ((map=route_get_map(this_->route))) {
1942                                 struct attr map_a;
1943                                 map_a.type=attr_map;
1944                                 map_a.u.map=map;
1945                                 mapset_add_attr(ms, &map_a);
1946                         }
1947                         if ((map=route_get_graph_map(this_->route))) {
1948                                 struct attr map_a,active;
1949                                 map_a.type=attr_map;
1950                                 map_a.u.map=map;
1951                                 active.type=attr_active;
1952                                 active.u.num=0;
1953                                 mapset_add_attr(ms, &map_a);
1954                                 map_set_attr(map, &active);
1955                         }
1956                         route_set_mapset(this_->route, ms);
1957                         route_set_projection(this_->route, transform_get_projection(this_->trans));
1958                 }
1959                 if (this_->tracking) {
1960                         tracking_set_mapset(this_->tracking, ms);
1961                         if (this_->route)
1962                                 tracking_set_route(this_->tracking, this_->route);
1963                 }
1964                 if (this_->navigation) {
1965                         if ((map=navigation_get_map(this_->navigation))) {
1966                                 struct attr map_a,active;
1967                                 map_a.type=attr_map;
1968                                 map_a.u.map=map;
1969                                 active.type=attr_active;
1970                                 active.u.num=0;
1971                                 mapset_add_attr(ms, &map_a);
1972                                 map_set_attr(map, &active);
1973                         }
1974                 }
1975                 if (this_->tracking) {
1976                         if ((map=tracking_get_map(this_->tracking))) {
1977                                 struct attr map_a,active;
1978                                 map_a.type=attr_map;
1979                                 map_a.u.map=map;
1980                                 active.type=attr_active;
1981                                 active.u.num=0;
1982                                 mapset_add_attr(ms, &map_a);
1983                                 map_set_attr(map, &active);
1984                         }
1985                 }
1986                 navit_add_former_destinations_from_file(this_);
1987         }
1988         if (this_->route) {
1989                 struct attr callback;
1990                 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1991                 callback.type=attr_callback;
1992                 callback.u.callback=this_->route_cb;
1993                 route_add_attr(this_->route, &callback);
1994         }
1995         if (this_->navigation) {
1996                 if (this_->speech) {
1997                         this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1998                         navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1999                 }
2000                 if (this_->route)
2001                         navigation_set_route(this_->navigation, this_->route);
2002         }
2003         dbg(2,"Setting Center\n");
2004         center_file = bookmarks_get_center_file(FALSE);
2005         bookmarks_set_center_from_file(this_->bookmarks, center_file);
2006         g_free(center_file);
2007 #if 0
2008         if (this_->menubar) {
2009                 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
2010                 if (men) {
2011                         navit_add_menu_windows_items(this_, men);
2012                 }
2013         }
2014 #endif
2015         global_navit=this_;
2016 #if 0
2017         navit_window_roadbook_new(this_);
2018         navit_window_items_new(this_);
2019 #endif
2020
2021         messagelist_init(this_->messages);
2022
2023         navit_set_cursors(this_);
2024
2025         callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
2026         callback=(this_->ready == 2);
2027         this_->ready|=1;
2028         dbg(2,"ready=%d\n",this_->ready);
2029         if (this_->ready == 3)
2030                 navit_draw_async(this_, 1);
2031         if (callback)
2032                 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
2033 #if 0
2034         routech_test(this_);
2035 #endif
2036 }
2037
2038 void
2039 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
2040 {
2041         struct coord c;
2042         int w,h,scale=16;
2043
2044         c.x=(r->rl.x+r->lu.x)/2;
2045         c.y=(r->rl.y+r->lu.y)/2;
2046         transform_set_center(this_->trans, &c);
2047         transform_get_size(this_->trans, &w, &h);
2048         dbg(0,"center 0x%x,0x%x w %d h %d\n",c.x,c.y,w,h);
2049         dbg(0,"%x,%x-%x,%x\n", r->lu.x,r->lu.y,r->rl.x,r->rl.y);
2050         while (scale < 1<<20) {
2051                 struct point p1,p2;
2052                 transform_set_scale(this_->trans, scale);
2053                 transform_setup_source_rect(this_->trans);
2054                 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
2055                 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
2056                 dbg(0,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
2057                 if (p1.x < 0 || p2.x < 0 || p1.x > w || p2.x > w ||
2058                     p1.y < 0 || p2.y < 0 || p1.y > h || p2.y > h)
2059                         scale*=2;
2060                 else
2061                         break;
2062         
2063         }
2064         dbg(0,"scale=%d (0x%x) of %d (0x%x)\n",scale,scale,1<<20,1<<20);
2065         if (this_->ready == 3)
2066                 navit_draw_async(this_,0);
2067 }
2068
2069 void
2070 navit_zoom_to_route(struct navit *this_, int orientation)
2071 {
2072         struct map *map;
2073         struct map_rect *mr=NULL;
2074         struct item *item;
2075         struct coord c;
2076         struct coord_rect r;
2077         int count=0;
2078         if (! this_->route)
2079                 return;
2080         dbg(1,"enter\n");
2081         map=route_get_map(this_->route);
2082         dbg(1,"map=%p\n",map);
2083         if (map)
2084                 mr=map_rect_new(map, NULL);
2085         dbg(1,"mr=%p\n",mr);
2086         if (mr) {
2087                 while ((item=map_rect_get_item(mr))) {
2088                         dbg(1,"item=%s\n", item_to_name(item->type));
2089                         while (item_coord_get(item, &c, 1)) {
2090                                 dbg(1,"coord\n");
2091                                 if (!count) 
2092                                         r.lu=r.rl=c;
2093                                 else
2094                                         coord_rect_extend(&r, &c);      
2095                                 count++;
2096                         }
2097                 }
2098                 map_rect_destroy(mr);
2099         }
2100         if (! count)
2101                 return;
2102         if (orientation != -1)
2103                 transform_set_yaw(this_->trans, orientation);
2104         navit_zoom_to_rect(this_, &r);
2105 }
2106
2107 static void
2108 navit_cmd_zoom_to_route(struct navit *this)
2109 {
2110         navit_zoom_to_route(this, 0);
2111 }
2112
2113
2114 /**
2115  * Change the current zoom level
2116  *
2117  * @param navit The navit instance
2118  * @param center The point where to center the map, including its projection
2119  * @returns nothing
2120  */
2121 void
2122 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
2123 {
2124         struct coord *c=transform_center(this_->trans);
2125         struct coord c1,c2;
2126         enum projection pro = transform_get_projection(this_->trans);
2127         if (pro != center->pro) {
2128                 c1.x = center->x;
2129                 c1.y = center->y;
2130                 transform_from_to(&c1, center->pro, &c2, pro);
2131         } else {
2132                 c2.x = center->x;
2133                 c2.y = center->y;
2134         }
2135         *c=c2;
2136         if (set_timeout) 
2137                 navit_set_timeout(this_);
2138         if (this_->ready == 3)
2139                 navit_draw(this_);
2140 }
2141
2142 static void
2143 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
2144 {
2145         int width, height;
2146         struct point po;
2147         transform_set_center(this_->trans, c);
2148         transform_get_size(this_->trans, &width, &height);
2149         po.x=width/2;
2150         po.y=height/2;
2151         update_transformation(this_->trans, &po, p, NULL);
2152         if (set_timeout)
2153                 navit_set_timeout(this_);
2154 }
2155
2156 /**
2157  * Links all vehicles to a cursor depending on the current profile.
2158  *
2159  * @param this_ A navit instance
2160  * @author Ralph Sennhauser (10/2009)
2161  */
2162 static void
2163 navit_set_cursors(struct navit *this_)
2164 {
2165         struct attr name;
2166         struct navit_vehicle *nv;
2167         struct cursor *c;
2168         GList *v;
2169
2170         v=g_list_first(this_->vehicles); // GList of navit_vehicles
2171         while (v) {
2172                 nv=v->data;
2173                 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
2174                         if (!strcmp(name.u.str,"none"))
2175                                 c=NULL;
2176                         else
2177                                 c=layout_get_cursor(this_->layout_current, name.u.str);
2178                 } else
2179                         c=layout_get_cursor(this_->layout_current, "default");
2180                 vehicle_set_cursor(nv->vehicle, c, 0);
2181                 v=g_list_next(v);
2182         }
2183         return;
2184 }
2185
2186 static int
2187 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
2188 {
2189         int width, height;
2190         struct navit_vehicle *nv=this_->vehicle;
2191
2192         float offset=this_->radius;      // Cursor offset from the center of the screen (percent).
2193 #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 */
2194         float min_offset = 0.;      // Percent offset at min_offset_speed.
2195         float max_offset = 30.;     // Percent offset at max_offset_speed.
2196         int min_offset_speed = 2;   // Speed in km/h
2197         int max_offset_speed = 50;  // Speed ini km/h
2198         // Calculate cursor offset from the center of the screen, upon speed.
2199         if (nv->speed <= min_offset_speed) {
2200             offset = min_offset;
2201         } else if (nv->speed > max_offset_speed) {
2202             offset = max_offset;
2203         } else {
2204             offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
2205         }
2206 #endif
2207
2208         transform_get_size(this_->trans, &width, &height);
2209         if (this_->orientation == -1 || keep_orientation) {
2210                 p->x=50*width/100;
2211                 p->y=(50 + offset)*height/100;
2212                 if (dir) 
2213                         *dir=keep_orientation?this_->orientation:nv->dir;
2214         } else {
2215                 int mdir;
2216                 if (this_->tracking && this_->tracking_flag) {
2217                         mdir = tracking_get_angle(this_->tracking) - this_->orientation;
2218                 } else {
2219                         mdir=nv->dir-this_->orientation;
2220                 }
2221
2222                 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
2223                 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
2224                 if (dir)
2225                         *dir=this_->orientation;
2226         }
2227         return 1;
2228 }
2229
2230 void
2231 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
2232 {
2233         int dir;
2234         struct point pn;
2235         struct navit_vehicle *nv=this_->vehicle;
2236         navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
2237         transform_set_yaw(this_->trans, dir);
2238         navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
2239         if (autozoom)
2240                 navit_autozoom(this_, &nv->coord, nv->speed, 0);
2241 }
2242
2243 static void
2244 navit_set_center_cursor_draw(struct navit *this_)
2245 {
2246         navit_set_center_cursor(this_,1,0);
2247         if (this_->ready == 3)
2248                 navit_draw_async(this_, 1);
2249 }
2250
2251 static void
2252 navit_cmd_set_center_cursor(struct navit *this_)
2253 {
2254         navit_set_center_cursor_draw(this_);
2255 }
2256
2257 void
2258 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
2259 {
2260         struct coord c;
2261         struct pcoord pc;
2262         transform_reverse(this_->trans, p, &c);
2263         pc.x = c.x;
2264         pc.y = c.y;
2265         pc.pro = transform_get_projection(this_->trans);
2266         navit_set_center(this_, &pc, set_timeout);
2267 }
2268
2269 static int
2270 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
2271 {
2272         int dir=0, orient_old=0, attr_updated=0;
2273         struct coord co;
2274         long zoom;
2275         GList *l;
2276         struct navit_vehicle *nv;
2277         struct layout *lay;
2278         struct attr active;
2279         active.type=attr_active;
2280         active.u.num=0;
2281
2282         switch (attr->type) {
2283         case attr_autozoom:
2284                 attr_updated=(this_->autozoom_secs != attr->u.num);
2285                 this_->autozoom_secs = attr->u.num;
2286                 break;
2287         case attr_autozoom_active:
2288                 attr_updated=(this_->autozoom_active != attr->u.num);
2289                 this_->autozoom_active = attr->u.num;
2290                 break;
2291         case attr_center:
2292                 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
2293                 dbg(1,"0x%x,0x%x\n",co.x,co.y);
2294                 transform_set_center(this_->trans, &co);
2295                 break;
2296         case attr_drag_bitmap:
2297                 attr_updated=(this_->drag_bitmap != !!attr->u.num);
2298                 this_->drag_bitmap=!!attr->u.num;
2299                 break;
2300         case attr_flags:
2301                 attr_updated=(this_->flags != attr->u.num);
2302                 this_->flags=attr->u.num;
2303                 break;
2304         case attr_flags_graphics:
2305                 attr_updated=(this_->graphics_flags != attr->u.num);
2306                 this_->graphics_flags=attr->u.num;
2307                 break;
2308         case attr_follow:
2309                 if (!this_->vehicle)
2310                         return 0;
2311                 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
2312                 this_->vehicle->follow_curr = attr->u.num;
2313                 break;
2314         case attr_layout:
2315                 if(this_->layout_current!=attr->u.layout) {
2316                         this_->layout_current=attr->u.layout;
2317                         graphics_font_destroy_all(this_->gra);
2318                         navit_set_cursors(this_);
2319                         if (this_->ready == 3)
2320                                 navit_draw(this_);
2321                         attr_updated=1;
2322                 }
2323                 break;
2324         case attr_layout_name:
2325                 l=this_->layouts;
2326                 while (l) {
2327                         lay=l->data;
2328                         if (!strcmp(lay->name,attr->u.str)) {
2329                                 struct attr attr;
2330                                 attr.type=attr_layout;
2331                                 attr.u.layout=lay;
2332                                 return navit_set_attr_do(this_, &attr, init);
2333                         }
2334                         l=g_list_next(l);
2335                 }               
2336                 return 0;
2337         case attr_map_border:
2338                 if (this_->border != attr->u.num) {
2339                         this_->border=attr->u.num;
2340                         attr_updated=1;
2341                 }
2342                 break;
2343         case attr_orientation:
2344                 orient_old=this_->orientation;
2345                 this_->orientation=attr->u.num;
2346                 if (!init) {
2347                         if (this_->orientation != -1) {
2348                                 dir = this_->orientation;
2349                         } else {
2350                                 if (this_->vehicle) {
2351                                         dir = this_->vehicle->dir;
2352                                 }
2353                         }
2354                         transform_set_yaw(this_->trans, dir);
2355                         if (orient_old != this_->orientation) {
2356 #if 0
2357                                 if (this_->ready == 3)
2358                                         navit_draw(this_);
2359 #endif
2360                                 attr_updated=1;
2361                         }
2362                 }
2363                 break;
2364         case attr_osd_configuration:
2365                 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
2366                 attr_updated=(this_->osd_configuration != attr->u.num);
2367                 this_->osd_configuration=attr->u.num;
2368                 break;
2369         case attr_pitch:
2370                 attr_updated=(this_->pitch != attr->u.num);
2371                 this_->pitch=attr->u.num;
2372                 transform_set_pitch(this_->trans, this_->pitch);
2373                 if (!init && attr_updated && this_->ready == 3)
2374                         navit_draw(this_);
2375                 break;
2376         case attr_projection:
2377                 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
2378                         navit_projection_set(this_, attr->u.projection, !init);
2379                         attr_updated=1;
2380                 }
2381                 break;
2382         case attr_radius:
2383                 attr_updated=(this_->radius != attr->u.num);
2384                 this_->radius=attr->u.num;
2385                 break;
2386         case attr_recent_dest:
2387                 attr_updated=(this_->recentdest_count != attr->u.num);
2388                 this_->recentdest_count=attr->u.num;
2389                 break;
2390         case attr_speech:
2391                 if(this_->speech && this_->speech != attr->u.speech) {
2392                         attr_updated=1;
2393                         this_->speech = attr->u.speech;
2394                 }
2395                 break;
2396         case attr_timeout:
2397                 attr_updated=(this_->center_timeout != attr->u.num);
2398                 this_->center_timeout = attr->u.num;
2399                 break;
2400         case attr_tracking:
2401                 attr_updated=(this_->tracking_flag != !!attr->u.num);
2402                 this_->tracking_flag=!!attr->u.num;
2403                 break;
2404         case attr_transformation:
2405                 this_->trans=attr->u.transformation;
2406                 break;
2407         case attr_use_mousewheel:
2408                 attr_updated=(this_->use_mousewheel != !!attr->u.num);
2409                 this_->use_mousewheel=!!attr->u.num;
2410                 break;
2411         case attr_vehicle:
2412                 l=this_->vehicles;
2413                 while(l) {
2414                         nv=l->data;
2415                         if (nv->vehicle == attr->u.vehicle) {
2416                                 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2417                                         if (this_->vehicle)
2418                                                 vehicle_set_attr(this_->vehicle->vehicle, &active);
2419                                         active.u.num=1;
2420                                         vehicle_set_attr(nv->vehicle, &active);
2421                                         attr_updated=1;
2422                                 }
2423                                 navit_set_vehicle(this_, nv);
2424                         }
2425                         l=g_list_next(l);
2426                 }
2427                 break;
2428         case attr_zoom:
2429                 zoom=transform_get_scale(this_->trans);
2430                 attr_updated=(zoom != attr->u.num);
2431                 transform_set_scale(this_->trans, attr->u.num);
2432                 if (attr_updated && !init) 
2433                         navit_draw(this_);
2434                 break;
2435         case attr_zoom_min:
2436                 attr_updated=(attr->u.num != this_->zoom_min);
2437                 this_->zoom_min=attr->u.num;
2438                 break;
2439         case attr_zoom_max:
2440                 attr_updated=(attr->u.num != this_->zoom_max);
2441                 this_->zoom_max=attr->u.num;
2442                 break;
2443         case attr_message:
2444                 navit_add_message(this_, attr->u.str);
2445                 break;
2446         case attr_follow_cursor:
2447                 attr_updated=(this_->follow_cursor != !!attr->u.num);
2448                 this_->follow_cursor=!!attr->u.num;
2449                 break;
2450         case attr_imperial: 
2451                 attr_updated=(this_->imperial != attr->u.num); 
2452                 this_->imperial=attr->u.num; 
2453                 break; 
2454         default:
2455                 return 0;
2456         }
2457         if (attr_updated && !init) {
2458                 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2459                 if (attr->type == attr_osd_configuration)
2460                         graphics_draw_mode(this_->gra, draw_mode_end);
2461         }
2462         return 1;
2463 }
2464
2465 int
2466 navit_set_attr(struct navit *this_, struct attr *attr)
2467 {
2468         return navit_set_attr_do(this_, attr, 0);
2469 }
2470
2471 int
2472 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2473 {
2474         struct message *msg;
2475         int len,offset;
2476         int ret=1;
2477
2478         switch (type) {
2479         case attr_message:
2480                 msg = navit_get_messages(this_);
2481                 
2482                 if (!msg) {
2483                         return 0;
2484                 }
2485
2486                 len = 0;
2487                 while (msg) {
2488                         len += strlen(msg->text) + 1;
2489                         msg = msg->next;
2490                 }
2491                 attr->u.str = g_malloc(len + 1);
2492                 
2493                 msg = navit_get_messages(this_);
2494                 offset = 0;
2495                 while (msg) {
2496                         g_stpcpy((attr->u.str + offset), msg->text);
2497                         offset += strlen(msg->text);
2498                         attr->u.str[offset] = '\n';
2499                         offset++;
2500
2501                         msg = msg->next;
2502                 }
2503
2504                 attr->u.str[len] = '\0';
2505                 break;
2506         case attr_imperial: 
2507                 attr->u.num=this_->imperial; 
2508                 break; 
2509         case attr_bookmark_map:
2510                 attr->u.map=bookmarks_get_map(this_->bookmarks);
2511                 break;
2512         case attr_bookmarks:
2513                 attr->u.bookmarks=this_->bookmarks;
2514                 break;
2515         case attr_callback_list:
2516                 attr->u.callback_list=this_->attr_cbl;
2517                 break;
2518         case attr_destination:
2519                 if (! this_->destination_valid)
2520                         return 0;
2521                 attr->u.pcoord=&this_->destination;
2522                 break;
2523         case attr_displaylist:
2524                 attr->u.displaylist=this_->displaylist;
2525                 return (attr->u.displaylist != NULL);
2526         case attr_follow:
2527                 if (!this_->vehicle)
2528                         return 0;
2529                 attr->u.num=this_->vehicle->follow_curr;
2530                 break;
2531         case attr_former_destination_map:
2532                 attr->u.map=this_->former_destination;
2533                 break;
2534         case attr_graphics:
2535                 attr->u.graphics=this_->gra;
2536                 ret=(attr->u.graphics != NULL);
2537                 break;
2538         case attr_gui:
2539                 attr->u.gui=this_->gui;
2540                 ret=(attr->u.gui != NULL);
2541                 break;
2542         case attr_layout:
2543                 if (iter) {
2544                         if (iter->u.list) {
2545                                 iter->u.list=g_list_next(iter->u.list);
2546                         } else { 
2547                                 iter->u.list=this_->layouts;
2548                         }
2549                         if (!iter->u.list)
2550                                 return 0;
2551                         attr->u.layout=(struct layout *)iter->u.list->data;
2552                 } else {
2553                         attr->u.layout=this_->layout_current;
2554                 }
2555                 break;
2556         case attr_map:
2557                 if (iter && this_->mapsets) {
2558                         if (!iter->u.mapset_handle) {
2559                                 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2560                         }
2561                         attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2562                         if(!attr->u.map) {
2563                                 mapset_close(iter->u.mapset_handle);
2564                                 return 0;
2565                         }
2566                 } else {
2567                         return 0;
2568                 }
2569                 break;
2570         case attr_mapset:
2571                 attr->u.mapset=this_->mapsets->data;
2572                 ret=(attr->u.mapset != NULL);
2573                 break;
2574         case attr_navigation:
2575                 attr->u.navigation=this_->navigation;
2576                 break;
2577         case attr_orientation:
2578                 attr->u.num=this_->orientation;
2579                 break;
2580         case attr_osd:
2581                 ret=attr_generic_get_attr(this_->attr_list, NULL, type, attr, iter?(struct attr_iter *)&iter->iter:NULL);
2582                 break;
2583         case attr_osd_configuration:
2584                 attr->u.num=this_->osd_configuration;
2585                 break;
2586         case attr_pitch:
2587                 attr->u.num=transform_get_pitch(this_->trans);
2588                 break;
2589         case attr_projection:
2590                 if(this_->trans) {
2591                         attr->u.num=transform_get_projection(this_->trans);
2592                 } else {
2593                         return 0;
2594                 }
2595                 break;
2596         case attr_route:
2597                 attr->u.route=this_->route;
2598                 break;
2599         case attr_speech:
2600                 attr->u.speech=this_->speech;
2601                 break;
2602         case attr_tracking:
2603                 attr->u.num=this_->tracking_flag;
2604                 break;
2605         case attr_trackingo:
2606                 attr->u.tracking=this_->tracking;
2607                 break;
2608         case attr_transformation:
2609                 attr->u.transformation=this_->trans;
2610                 break;
2611         case attr_vehicle:
2612                 if(iter) {
2613                         if(iter->u.list) {
2614                                 iter->u.list=g_list_next(iter->u.list);
2615                         } else { 
2616                                 iter->u.list=this_->vehicles;
2617                         }
2618                         if(!iter->u.list)
2619                                 return 0;
2620                         attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2621                 } else {
2622                         if(this_->vehicle) {
2623                                 attr->u.vehicle=this_->vehicle->vehicle;
2624                         } else {
2625                                 return 0;
2626                         }
2627                 }
2628                 break;
2629         case attr_vehicleprofile:
2630                 attr->u.vehicleprofile=this_->vehicleprofile;
2631                 break;
2632         case attr_zoom:
2633                 attr->u.num=transform_get_scale(this_->trans);
2634                 break;
2635         case attr_autozoom_active:
2636                 attr->u.num=this_->autozoom_active;
2637                 break;
2638         case attr_follow_cursor:
2639                 attr->u.num=this_->follow_cursor;
2640                 break;
2641         default:
2642                 return 0;
2643         }
2644         attr->type=type;
2645         return ret;
2646 }
2647
2648 static int
2649 navit_add_log(struct navit *this_, struct log *log)
2650 {
2651         struct attr type_attr;
2652         if (!log_get_attr(log, attr_type, &type_attr, NULL))
2653                 return 0;
2654         if (!strcmp(type_attr.u.str, "textfile_debug")) {
2655                 char *header = "type=track_tracked\n";
2656                 if (this_->textfile_debug_log)
2657                         return 0;
2658                 log_set_header(log, header, strlen(header));
2659                 this_->textfile_debug_log=log;
2660                 return 1;
2661         }
2662         return 0;
2663 }
2664
2665 static int
2666 navit_add_layout(struct navit *this_, struct layout *layout)
2667 {
2668         struct attr active;
2669         this_->layouts = g_list_append(this_->layouts, layout);
2670         layout_get_attr(layout, attr_active, &active, NULL);
2671         if(active.u.num || !this_->layout_current) { 
2672                 this_->layout_current=layout;
2673                 return 1;
2674         }
2675         return 0;
2676 }
2677
2678 int
2679 navit_add_attr(struct navit *this_, struct attr *attr)
2680 {
2681         int ret=1;
2682         switch (attr->type) {
2683         case attr_callback:
2684                 navit_add_callback(this_, attr->u.callback);
2685                 break;
2686         case attr_log:
2687                 ret=navit_add_log(this_, attr->u.log);
2688                 break;
2689         case attr_gui:
2690                 ret=navit_set_gui(this_, attr->u.gui);
2691                 break;
2692         case attr_graphics:
2693                 ret=navit_set_graphics(this_, attr->u.graphics);
2694                 break;
2695         case attr_layout:
2696                 ret=navit_add_layout(this_, attr->u.layout);
2697                 break;
2698         case attr_route:
2699                 this_->route=attr->u.route;
2700                 break;
2701         case attr_mapset:
2702                 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2703                 break;
2704         case attr_navigation:
2705                 this_->navigation=attr->u.navigation;
2706                 break;
2707         case attr_osd:
2708                 this_->attr_list=attr_generic_add_attr(this_->attr_list, attr);
2709                 break;
2710         case attr_recent_dest:
2711                 this_->recentdest_count = attr->u.num;
2712                 break;
2713         case attr_speech:
2714                 this_->speech=attr->u.speech;
2715                 break;
2716         case attr_trackingo:
2717                 this_->tracking=attr->u.tracking;
2718                 break;
2719         case attr_vehicle:
2720                 ret=navit_add_vehicle(this_, attr->u.vehicle);
2721                 break;
2722         case attr_vehicleprofile:
2723                 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2724                 break;
2725         case attr_autozoom_min:
2726                 this_->autozoom_min = attr->u.num;
2727                 break;
2728         default:
2729                 return 0;
2730         }
2731         if (ret)
2732                 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
2733         callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2734         return ret;
2735 }
2736
2737 int
2738 navit_remove_attr(struct navit *this_, struct attr *attr)
2739 {
2740         int ret=1;
2741         switch (attr->type) {
2742         case attr_callback:
2743                 navit_remove_callback(this_, attr->u.callback);
2744                 break;
2745         case attr_vehicle:
2746                 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
2747                 return 1;
2748         default:
2749                 return 0;
2750         }
2751         return ret;
2752 }
2753
2754 struct attr_iter *
2755 navit_attr_iter_new(void)
2756 {
2757         return g_new0(struct attr_iter, 1);
2758 }
2759
2760 void
2761 navit_attr_iter_destroy(struct attr_iter *iter)
2762 {
2763         g_free(iter);
2764 }
2765
2766 void
2767 navit_add_callback(struct navit *this_, struct callback *cb)
2768 {
2769         callback_list_add(this_->attr_cbl, cb);
2770 }
2771
2772 void
2773 navit_remove_callback(struct navit *this_, struct callback *cb)
2774 {
2775         callback_list_remove(this_->attr_cbl, cb);
2776 }
2777
2778 /**
2779  * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2780  *
2781  * @param navit The navit instance
2782  * @returns nothing
2783  */
2784
2785 static void
2786 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2787 {
2788         struct point cursor_pnt;
2789         enum projection pro;
2790
2791         if (this_->blocked)
2792                 return;
2793         if (pnt)
2794                 cursor_pnt=*pnt;
2795         else {
2796                 pro=transform_get_projection(this_->trans_cursor);
2797                 if (!pro)
2798                         return;
2799                 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2800         }
2801         vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2802 #if 0   
2803         if (pnt)
2804                 pnt2=*pnt;
2805         else {
2806                 pro=transform_get_projection(this_->trans);
2807                 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2808         }
2809 #if 1
2810         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2811 #else
2812         cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2813 #endif
2814 #endif
2815 }
2816
2817 static void
2818 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2819 {
2820         struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2821         struct pcoord cursor_pc;
2822         struct point cursor_pnt, *pnt=&cursor_pnt;
2823         struct tracking *tracking=NULL;
2824         struct pcoord pc[16];
2825         enum projection pro=transform_get_projection(this_->trans_cursor);
2826         int count;
2827         int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2828         void *attr_object;
2829         char *destination_file;
2830
2831         profile(0,NULL);
2832         if (this_->ready != 3) {
2833                 profile(0,"return 1\n");
2834                 return;
2835         }
2836         navit_layout_switch(this_);
2837         if (this_->vehicle == nv && this_->tracking_flag)
2838                 tracking=this_->tracking;
2839         if (tracking) {
2840                 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2841                 attr_object=tracking;
2842                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2843         } else {
2844                 attr_object=nv->vehicle;
2845                 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2846         }
2847         if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2848                 if (!attr_valid.u.num != attr_position_valid_invalid)
2849                         return;
2850         if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2851             ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2852             ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2853                 profile(0,"return 2\n");
2854                 return;
2855         }
2856         nv->dir=*attr_dir.u.numd;
2857         nv->speed=*attr_speed.u.numd;
2858         transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2859         if (nv != this_->vehicle) {
2860                 navit_vehicle_draw(this_, nv, NULL);
2861                 profile(0,"return 3\n");
2862                 return;
2863         }
2864         cursor_pc.x = nv->coord.x;
2865         cursor_pc.y = nv->coord.y;
2866         cursor_pc.pro = pro;
2867         if (this_->route) {
2868                 if (tracking)
2869                         route_set_position_from_tracking(this_->route, tracking, pro);
2870                 else
2871                         route_set_position(this_->route, &cursor_pc);
2872         }
2873         callback_list_call_attr_0(this_->attr_cbl, attr_position);
2874         navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2875         if (this_->gui && nv->speed > 2)
2876                 navit_disable_suspend();
2877
2878         transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2879         if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow && 
2880                 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2881                 navit_set_center_cursor_draw(this_);
2882         else
2883                 navit_vehicle_draw(this_, nv, pnt);
2884
2885         if (nv->follow_curr > 1)
2886                 nv->follow_curr--;
2887         else
2888                 nv->follow_curr=nv->follow;
2889         callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2890
2891         /* Finally, if we reached our destination, stop navigation. */
2892         if (this_->route) {
2893                 switch(route_destination_reached(this_->route)) {
2894                 case 1:
2895                         route_remove_waypoint(this_->route);
2896                         count=route_get_destinations(this_->route, pc, 16);
2897                         destination_file = bookmarks_get_destination_file(TRUE);
2898                         bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, NULL, this_->recentdest_count);
2899                         break;  
2900                 case 2:
2901                         navit_set_destination(this_, NULL, NULL, 0);
2902                         break;
2903                 }
2904         }
2905         profile(0,"return 5\n");
2906 }
2907
2908 /**
2909  * Set the position of the vehicle
2910  *
2911  * @param navit The navit instance
2912  * @param c The coordinate to set as position
2913  * @returns nothing
2914  */
2915
2916 void
2917 navit_set_position(struct navit *this_, struct pcoord *c)
2918 {
2919         if (this_->route) {
2920                 route_set_position(this_->route, c);
2921                 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2922         }
2923         if (this_->ready == 3)
2924                 navit_draw(this_);
2925 }
2926
2927 static int
2928 navit_set_vehicleprofile(struct navit *this_, char *name)
2929 {
2930         struct attr attr;
2931         GList *l;
2932         l=this_->vehicleprofiles;
2933         while (l) {
2934                 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2935                         if (!strcmp(attr.u.str, name)) {
2936                                 this_->vehicleprofile=l->data;
2937                                 if (this_->route)
2938                                         route_set_profile(this_->route, this_->vehicleprofile);
2939                                 return 1;
2940                         }
2941                 }
2942                 l=g_list_next(l);
2943         }
2944         return 0;
2945 }
2946
2947 static void
2948 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2949 {
2950         struct attr attr;
2951         this_->vehicle=nv;
2952         if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2953                 if (navit_set_vehicleprofile(this_, attr.u.str))
2954                         return;
2955         }
2956         if (!navit_set_vehicleprofile(this_,"car")) {
2957                 /* We do not have a fallback "car" profile
2958                 * so lets set any profile */
2959                 GList *l;
2960                 l=this_->vehicleprofiles;
2961                 if (l) {
2962                     this_->vehicleprofile=l->data;
2963                     if (this_->route)
2964                         route_set_profile(this_->route, this_->vehicleprofile);
2965                 }
2966         }
2967 }
2968
2969 /**
2970  * Register a new vehicle
2971  *
2972  * @param navit The navit instance
2973  * @param v The vehicle instance
2974  * @returns 1 for success
2975  */
2976 static int
2977 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2978 {
2979         struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2980         struct attr follow, active, animate;
2981         nv->vehicle=v;
2982         nv->follow=0;
2983         nv->last.x = 0;
2984         nv->last.y = 0;
2985         nv->animate_cursor=0;
2986         if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2987                 nv->follow=follow.u.num;
2988         nv->follow_curr=nv->follow;
2989         this_->vehicles=g_list_append(this_->vehicles, nv);
2990         if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2991                 navit_set_vehicle(this_, nv);
2992         if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2993                 nv->animate_cursor=animate.u.num;
2994         nv->callback.type=attr_callback;
2995         nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2996         vehicle_add_attr(nv->vehicle, &nv->callback);
2997         vehicle_set_attr(nv->vehicle, &this_->self);
2998         return 1;
2999 }
3000
3001
3002
3003
3004 struct gui *
3005 navit_get_gui(struct navit *this_)
3006 {
3007         return this_->gui;
3008 }
3009
3010 struct transformation *
3011 navit_get_trans(struct navit *this_)
3012 {
3013         return this_->trans;
3014 }
3015
3016 struct route *
3017 navit_get_route(struct navit *this_)
3018 {
3019         return this_->route;
3020 }
3021
3022 struct navigation *
3023 navit_get_navigation(struct navit *this_)
3024 {
3025         return this_->navigation;
3026 }
3027
3028 struct displaylist *
3029 navit_get_displaylist(struct navit *this_)
3030 {
3031         return this_->displaylist;
3032 }
3033
3034 void
3035 navit_layout_switch(struct navit *n) 
3036 {
3037
3038     int currTs=0;
3039     struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
3040     double trise,tset,trise_actual;
3041     struct layout *l;
3042     int year, month, day;
3043     
3044     if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
3045         return; //No layout - nothing to switch
3046     }
3047     if (!n->vehicle)
3048         return;
3049     l=layout_attr.u.layout;
3050     
3051     if (l->dayname || l->nightname) {
3052         //Ok, we know that we have profile to switch
3053         
3054         //Check that we aren't calculating too fast
3055         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
3056                 currTs=iso8601_to_secs(iso8601_attr.u.str);
3057                 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
3058         }
3059         if (currTs-(n->prevTs)<60) {
3060             //We've have to wait a little
3061             return;
3062         }
3063         if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
3064                 return;
3065         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
3066                 return; //No valid fix yet
3067         }
3068         if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
3069                 //No position - no sun
3070                 return;
3071         }
3072         
3073         //We calculate sunrise anyway, cause it is needed both for day and for night
3074         if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3075                 //near the pole sun never rises/sets, so we should never switch profiles
3076                 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
3077                 n->prevTs=currTs;
3078                 return;
3079             }
3080         
3081         trise_actual=trise;
3082         dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
3083         if (l->dayname) {
3084         
3085             if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) || 
3086                     (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
3087                 //The sun is rising now!
3088                 if (strcmp(l->name,l->dayname)) {
3089                     navit_set_layout_by_name(n,l->dayname);
3090                 }
3091             }
3092         }
3093         if (l->nightname) {
3094             if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3095                 //near the pole sun never rises/sets, so we should never switch profiles
3096                 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
3097                 n->prevTs=currTs;
3098                 return;
3099             }
3100             dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
3101             if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
3102                 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) || 
3103                         ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
3104                 //Time to sleep
3105                 if (strcmp(l->name,l->nightname)) {
3106                     navit_set_layout_by_name(n,l->nightname);
3107                 }
3108             }   
3109         }
3110         
3111         n->prevTs=currTs;
3112     }
3113 }
3114
3115 int 
3116 navit_set_vehicle_by_name(struct navit *n,const char *name) 
3117 {
3118     struct vehicle *v;
3119     struct attr_iter *iter;
3120     struct attr vehicle_attr, name_attr;
3121
3122         iter=navit_attr_iter_new();
3123
3124     while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
3125                 v=vehicle_attr.u.vehicle;
3126                 vehicle_get_attr(v,attr_name,&name_attr,NULL);
3127                 if (name_attr.type==attr_name) {
3128                         if (!strcmp(name,name_attr.u.str)) {
3129                                 navit_set_attr(n,&vehicle_attr);                                
3130                                 navit_attr_iter_destroy(iter);
3131                                 return 1;
3132                         }
3133                 }
3134         }
3135     navit_attr_iter_destroy(iter);
3136     return 0;
3137 }
3138
3139 int 
3140 navit_set_layout_by_name(struct navit *n,const char *name) 
3141 {
3142     struct layout *l;
3143     struct attr_iter iter;
3144     struct attr layout_attr;
3145
3146     iter.u.list=0x00;
3147
3148     if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
3149         return 0; //No layouts - nothing to do
3150     }
3151     if (iter.u.list==NULL) {
3152         return 0;
3153     }
3154     
3155     iter.u.list=g_list_first(iter.u.list);
3156     
3157     while(iter.u.list) {
3158         l=(struct layout*)iter.u.list->data;
3159         if (!strcmp(name,l->name)) {
3160             layout_attr.u.layout=l;
3161             layout_attr.type=attr_layout;
3162             navit_set_attr(n,&layout_attr);
3163             iter.u.list=g_list_first(iter.u.list);
3164             return 1;
3165         }
3166         iter.u.list=g_list_next(iter.u.list);
3167     }
3168
3169     iter.u.list=g_list_first(iter.u.list);
3170     return 0;
3171 }
3172
3173 void
3174 navit_disable_suspend() {
3175         gui_disable_suspend(global_navit->gui);
3176         callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
3177 }
3178
3179 int
3180 navit_block(struct navit *this_, int block)
3181 {
3182         if (block > 0) {
3183                 this_->blocked |= 1;
3184                 if (graphics_draw_cancel(this_->gra, this_->displaylist))
3185                         this_->blocked |= 2;
3186                 return 0;
3187         }
3188         if ((this_->blocked & 2) || block < 0) {
3189                 this_->blocked=0;
3190                 navit_draw(this_);
3191                 return 1;
3192         }
3193         this_->blocked=0;
3194         return 0;
3195 }
3196
3197 void
3198 navit_destroy(struct navit *this_)
3199 {
3200         dbg(0,"enter %p\n",this_);
3201         graphics_draw_cancel(this_->gra, this_->displaylist);
3202         callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
3203         attr_list_free(this_->attrs);
3204         if (this_->bookmarks) {
3205                 char *center_file = bookmarks_get_center_file(TRUE);
3206                 bookmarks_write_center_to_file(this_->bookmarks, center_file);
3207                 g_free(center_file);
3208                 bookmarks_destroy(this_->bookmarks);
3209         }
3210
3211         callback_destroy(this_->nav_speech_cb);
3212         callback_destroy(this_->roadbook_callback);
3213         callback_destroy(this_->popup_callback);
3214         callback_destroy(this_->motion_timeout_callback);
3215         callback_destroy(this_->progress_cb);
3216
3217         if(this_->gra) {
3218           graphics_remove_callback(this_->gra, this_->resize_callback);
3219           graphics_remove_callback(this_->gra, this_->button_callback);
3220           graphics_remove_callback(this_->gra, this_->motion_callback);
3221           graphics_remove_callback(this_->gra, this_->predraw_callback);
3222         }
3223
3224         callback_destroy(this_->resize_callback);
3225         callback_destroy(this_->motion_callback);
3226         callback_destroy(this_->predraw_callback);
3227
3228         callback_destroy(this_->route_cb);
3229         if (this_->route)
3230                 route_destroy(this_->route);
3231
3232         map_destroy(this_->former_destination);
3233
3234         graphics_displaylist_destroy(this_->displaylist);
3235
3236         graphics_free(this_->gra);
3237
3238         g_free(this_);
3239 }
3240
3241 struct navit *
3242 navit_ref(struct navit *this_)
3243 {
3244         this_->refcount++;
3245         dbg(0,"refcount %d\n",this_->refcount);
3246         return this_;
3247 }
3248
3249 void
3250 navit_unref(struct navit *this_)
3251 {
3252         this_->refcount--;
3253         dbg(0,"refcount %d\n",this_->refcount);
3254         if (this_->refcount <= 0)
3255                 navit_destroy(this_);
3256 }
3257
3258 struct object_func navit_func = {
3259         attr_navit,
3260         (object_func_new)navit_new,
3261         (object_func_get_attr)navit_get_attr,
3262         (object_func_iter_new)navit_attr_iter_new,
3263         (object_func_iter_destroy)navit_attr_iter_destroy,
3264         (object_func_set_attr)navit_set_attr,
3265         (object_func_add_attr)navit_add_attr,
3266         (object_func_remove_attr)navit_remove_attr,
3267         (object_func_init)navit_init,
3268         (object_func_destroy)navit_destroy,
3269         (object_func_dup)NULL,
3270         (object_func_ref)navit_ref,
3271         (object_func_unref)navit_unref,
3272 };
3273
3274 /** @} */