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