2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2009 Navit Team
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.
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.
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.
35 #include "projection.h"
41 #include "transform.h"
46 #include "data_window.h"
48 #include "navigation.h"
59 #include "navit_nls.h"
62 #include "vehicleprofile.h"
66 * @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
70 //! The navit_vehicule
71 struct navit_vehicle {
73 /*! Limit of the follow counter. See navit_add_vehicle */
75 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
79 struct coord last; /*< Position of the last update of this vehicle */
80 struct vehicle *vehicle;
90 struct layout *layout_current;
92 struct action *action;
93 struct transformation *trans;
94 struct compass *compass;
96 struct navigation *navigation;
97 struct speech *speech;
98 struct tracking *tracking;
101 struct displaylist *displaylist;
104 int recentdest_count;
105 int osd_configuration;
107 GList *windows_items;
108 struct navit_vehicle *vehicle;
109 struct callback_list *attr_cbl;
110 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb;
111 struct datawindow *roadbook_window;
112 struct map *bookmark;
113 struct map *former_destination;
114 GHashTable *bookmarks_hash;
115 struct point pressed, last, current;
116 int button_pressed,moved,popped,zoomed;
121 struct event_timeout *button_timeout, *motion_timeout;
122 struct callback *motion_timeout_callback;
124 int ignore_graphics_events;
125 struct log *textfile_debug_log;
126 struct pcoord destination;
127 int destination_valid;
132 struct messagelist *messages;
133 struct callback *resize_callback,*button_callback,*motion_callback;
134 struct vehicleprofile *vehicleprofile;
135 GList *vehicleprofiles;
140 int zoom_min, zoom_max;
143 struct gui *main_loop_gui;
148 struct mapset_handle *mapset_handle;
152 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
153 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
154 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
155 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
156 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir);
157 static void navit_set_cursors(struct navit *this_);
158 static void navit_cmd_zoom_to_route(struct navit *this);
159 static void navit_cmd_set_center_cursor(struct navit *this_);
160 static void navit_cmd_announcer_toggle(struct navit *this_);
161 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
164 navit_add_mapset(struct navit *this_, struct mapset *ms)
166 this_->mapsets = g_list_append(this_->mapsets, ms);
170 navit_get_mapset(struct navit *this_)
173 return this_->mapsets->data;
175 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
181 navit_get_tracking(struct navit *this_)
183 return this_->tracking;
187 navit_draw_async(struct navit *this_, int async)
190 struct navit_vehicle *nv;
192 if (this_->blocked) {
196 transform_setup_source_rect(this_->trans);
200 navit_vehicle_draw(this_, nv, NULL);
203 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
207 navit_draw(struct navit *this_)
209 navit_draw_async(this_, 0);
213 navit_get_ready(struct navit *this_)
221 navit_draw_displaylist(struct navit *this_)
223 if (this_->ready == 3)
224 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
228 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
231 if (attr->type != attr_route_status)
234 if (this_->ready != 3)
236 if (updated != route_status_path_done_new)
238 if (this_->vehicle) {
239 if (this_->vehicle->follow_curr == 1)
241 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
242 this_->vehicle->follow_curr=this_->vehicle->follow;
248 navit_handle_resize(struct navit *this_, int w, int h)
250 struct map_selection sel;
251 int callback=(this_->ready == 1);
253 if (this_->w != w || this_->h != h) {
254 memset(&sel, 0, sizeof(sel));
259 transform_set_screen_selection(this_->trans, &sel);
260 graphics_init(this_->gra);
261 graphics_set_rect(this_->gra, &sel.u.p_rect);
264 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
265 if (this_->ready == 3)
270 navit_resize(void *data, int w, int h)
272 struct navit *this=data;
273 if (!this->ignore_graphics_events)
274 navit_handle_resize(this, w, h);
278 navit_get_width(struct navit *this_)
285 navit_get_height(struct navit *this_)
291 navit_popup(void *data)
293 struct navit *this_=data;
294 popup(this_, 1, &this_->pressed);
295 this_->button_timeout=NULL;
301 navit_ignore_button(struct navit *this_)
303 if (this_->ignore_button)
305 this_->ignore_button=1;
310 navit_ignore_graphics_events(struct navit *this_, int ignore)
312 this_->ignore_graphics_events=ignore;
316 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
321 double angleo,anglen;
323 if (!transform_reverse(tr, old, &co))
326 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
327 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
328 yaw=transform_get_yaw(tr)+angleo-anglen;
329 transform_set_yaw(tr, yaw % 360);
331 if (!transform_reverse(tr, new, &cn))
333 cp=transform_get_center(tr);
336 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
337 transform_set_center(tr, &c);
341 navit_set_timeout(struct navit *this_)
344 follow.type=attr_follow;
345 follow.u.num=this_->center_timeout;
346 navit_set_attr(this_, &follow);
350 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
354 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
355 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
356 if (this_->ignore_button) {
357 this_->ignore_button=0;
365 this_->button_pressed=1;
369 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
372 navit_set_center_screen(this_, p, 1);
374 popup(this_, button, p);
375 if (button == 4 && this_->use_mousewheel) {
377 navit_zoom_in(this_, 2, p);
379 if (button == 5 && this_->use_mousewheel) {
381 navit_zoom_out(this_, 2, p);
385 this_->button_pressed=0;
386 if (this_->button_timeout) {
387 event_remove_timeout(this_->button_timeout);
388 this_->button_timeout=NULL;
389 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
390 navit_set_center_screen(this_, p, !this_->zoomed);
393 if (this_->motion_timeout) {
394 event_remove_timeout(this_->motion_timeout);
395 this_->motion_timeout=NULL;
402 update_transformation(this_->trans, &this_->pressed, p, &pr);
404 update_transformation(this_->trans, &this_->pressed, p, NULL);
406 graphics_draw_drag(this_->gra, NULL);
407 graphics_overlay_disable(this_->gra, 0);
409 navit_set_timeout(this_);
418 navit_button(void *data, int pressed, int button, struct point *p)
420 struct navit *this=data;
421 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
422 if (!this->ignore_graphics_events) {
423 if (! this->popup_callback)
424 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
425 navit_handle_button(this, pressed, button, p, this->popup_callback);
431 navit_motion_timeout(struct navit *this_)
435 if (this_->drag_bitmap) {
437 point.x=(this_->current.x-this_->pressed.x);
438 point.y=(this_->current.y-this_->pressed.y);
439 if (graphics_draw_drag(this_->gra, &point)) {
440 graphics_overlay_disable(this_->gra, 1);
441 graphics_draw_mode(this_->gra, draw_mode_end);
443 this_->motion_timeout=NULL;
447 dx=(this_->current.x-this_->last.x);
448 dy=(this_->current.y-this_->last.y);
450 struct transformation *tr;
452 this_->last=this_->current;
453 graphics_overlay_disable(this_->gra, 1);
454 tr=transform_dup(this_->trans);
458 update_transformation(tr, &this_->pressed, &this_->current, &pr);
460 update_transformation(tr, &this_->pressed, &this_->current, NULL);
463 graphics_displaylist_move(this_->displaylist, dx, dy);
465 graphics_draw_cancel(this_->gra, this_->displaylist);
466 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
467 transform_destroy(tr);
470 this_->motion_timeout=NULL;
475 navit_handle_motion(struct navit *this_, struct point *p)
479 if (this_->button_pressed && !this_->popped) {
480 dx=(p->x-this_->pressed.x);
481 dy=(p->y-this_->pressed.y);
482 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
484 if (this_->button_timeout) {
485 event_remove_timeout(this_->button_timeout);
486 this_->button_timeout=NULL;
489 if (! this_->motion_timeout_callback)
490 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
491 if (! this_->motion_timeout)
492 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
498 navit_motion(void *data, struct point *p)
500 struct navit *this=data;
501 if (!this->ignore_graphics_events)
502 navit_handle_motion(this, p);
506 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
508 struct coord c1, c2, *center;
509 if (scale < this_->zoom_min)
510 scale=this_->zoom_min;
511 if (scale > this_->zoom_max)
512 scale=this_->zoom_max;
514 transform_reverse(this_->trans, p, &c1);
515 transform_set_scale(this_->trans, scale);
517 transform_reverse(this_->trans, p, &c2);
518 center = transform_center(this_->trans);
519 center->x += c1.x - c2.x;
520 center->y += c1.y - c2.y;
527 * @brief Automatically adjusts zoom level
529 * This function automatically adjusts the current
530 * zoom level according to the current speed.
532 * @param this_ The navit struct
533 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
534 * @param speed The vehicles speed in meters per second
535 * @param dir The direction into which the vehicle moves
538 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
545 if (! this_->autozoom_active) {
549 distance = speed * this_->autozoom_secs;
551 transform_get_size(this_->trans, &w, &h);
552 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
553 scale = transform_get_scale(this_->trans);
555 /* We make sure that the point we want to see is within a certain range
556 * around the vehicle. The radius of this circle is the size of the
557 * screen. This doesn't necessarily mean the point is visible because of
558 * perspective etc. Quite rough, but should be enough. */
561 new_scale = (double)distance / h * 16;
563 new_scale = (double)distance / w * 16;
566 if (abs(new_scale - scale) < 2) {
570 if (new_scale >= this_->autozoom_min) {
571 navit_scale(this_, (long)new_scale, &pc, 0);
573 if (scale != this_->autozoom_min) {
574 navit_scale(this_, this_->autozoom_min, &pc, 0);
580 * Change the current zoom level, zooming closer to the ground
582 * @param navit The navit instance
583 * @param factor The zoom factor, usually 2
584 * @param p The invariant point (if set to NULL, default to center)
588 navit_zoom_in(struct navit *this_, int factor, struct point *p)
590 long scale=transform_get_scale(this_->trans)/factor;
593 navit_scale(this_, scale, p, 1);
597 * Change the current zoom level
599 * @param navit The navit instance
600 * @param factor The zoom factor, usually 2
601 * @param p The invariant point (if set to NULL, default to center)
605 navit_zoom_out(struct navit *this_, int factor, struct point *p)
607 long scale=transform_get_scale(this_->trans)*factor;
608 navit_scale(this_, scale, p, 1);
612 navit_cmd_zoom_in(struct navit *this_)
615 if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
616 navit_zoom_in(this_, 2, &p);
617 this_->vehicle->follow_curr=this_->vehicle->follow;
619 navit_zoom_in(this_, 2, NULL);
624 navit_cmd_zoom_out(struct navit *this_)
627 if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
628 navit_zoom_out(this_, 2, &p);
629 this_->vehicle->follow_curr=this_->vehicle->follow;
631 navit_zoom_out(this_, 2, NULL);
637 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
639 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
640 navit_say(this, in[0]->u.str);
644 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
647 char *description=NULL;
652 pc.pro = transform_get_projection(this->trans);
653 if (ATTR_IS_COORD(in[0]->type)) {
654 pc.x=in[0]->u.coord->x;
655 pc.y=in[0]->u.coord->y;
657 } else if (ATTR_IS_PCOORD(in[0]->type)) {
660 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
665 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
671 if (in[0] && ATTR_IS_STRING(in[0]->type))
672 description=in[0]->u.str;
673 navit_set_destination(this, &pc, description, 1);
676 static struct command_table commands[] = {
677 {"zoom_in",command_cast(navit_cmd_zoom_in)},
678 {"zoom_out",command_cast(navit_cmd_zoom_out)},
679 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
680 {"say",command_cast(navit_cmd_say)},
681 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
682 {"set_destination",command_cast(navit_cmd_set_destination)},
683 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
688 navit_new(struct attr *parent, struct attr **attrs)
690 struct navit *this_=g_new0(struct navit, 1);
691 struct pcoord center;
694 enum projection pro=projection_mg;
699 this_->self.type=attr_navit;
700 this_->self.u.navit=this_;
701 this_->attr_cbl=callback_list_new();
703 this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
705 this_->orientation=-1;
706 this_->tracking_flag=1;
707 this_->recentdest_count=10;
708 this_->osd_configuration=-1;
710 this_->center_timeout = 10;
711 this_->use_mousewheel = 1;
712 this_->autozoom_secs = 10;
713 this_->autozoom_min = 7;
714 this_->autozoom_active = 0;
716 this_->zoom_max = 2097152;
717 this_->follow_cursor = 1;
719 this_->trans = transform_new();
720 transform_from_geo(pro, &g, &co);
727 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
728 for (;*attrs; attrs++) {
729 navit_set_attr_do(this_, *attrs, 1);
731 this_->displaylist=graphics_displaylist_new();
732 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
734 this_->messages = messagelist_new(attrs);
740 navit_set_gui(struct navit *this_, struct gui *gui)
745 if (gui_has_main_loop(this_->gui)) {
746 if (! main_loop_gui) {
747 main_loop_gui=this_->gui;
749 dbg(0,"gui with main loop already active, ignoring this instance");
757 navit_add_message(struct navit *this_, char *message)
759 message_new(this_->messages, message);
763 *navit_get_messages(struct navit *this_)
765 return message_get(this_->messages);
769 navit_set_graphics(struct navit *this_, struct graphics *gra)
774 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
775 graphics_add_callback(gra, this_->resize_callback);
776 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
777 graphics_add_callback(gra, this_->button_callback);
778 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
779 graphics_add_callback(gra, this_->motion_callback);
784 navit_get_graphics(struct navit *this_)
789 struct vehicleprofile *
790 navit_get_vehicleprofile(struct navit *this_)
792 return this_->vehicleprofile;
796 navit_get_vehicleprofiles(struct navit *this_)
798 return this_->vehicleprofiles;
802 navit_projection_set(struct navit *this_, enum projection pro)
807 c=transform_center(this_->trans);
808 transform_to_geo(transform_get_projection(this_->trans), c, &g);
809 transform_set_projection(this_->trans, pro);
810 transform_from_geo(pro, &g, c);
815 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
818 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
833 while ((ch = fgetc(f)) != EOF) {
834 if ((ch == '\n') && (prev != '\n')) {
840 if (prev != '\n') { // Last line did not end with a newline
845 f = fopen(file, "r+");
847 while (lines >= limit) { // We have to "scroll up"
849 numc = 0; // Counts how many bytes we have in our line to scroll up
850 while ((ch = fgetc(f)) != EOF) {
857 buffer=g_malloc(numc);
858 offset = numc; // Offset holds where we currently are
861 fseek(f,offset,SEEK_SET);
862 readc = fread(buffer,1,numc,f);
864 fseek(f,-(numc+readc),SEEK_CUR);
865 fwrite(buffer,1,readc,f);
868 } while (readc == numc);
872 ftruncate(fd,(offset-numc));
886 prostr = projection_to_name(c->pro,NULL);
887 fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
888 prostr, *prostr ? ":" : "",
889 c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
890 c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y,
899 * navit_get_user_data_directory
901 * returns the directory used to store user data files (center.txt,
902 * destination.txt, bookmark.txt, ...)
904 * arg: gboolean create: create the directory if it does not exist
907 navit_get_user_data_directory(gboolean create) {
909 dir = getenv("NAVIT_USER_DATADIR");
910 if (create && !file_exists(dir)) {
911 dbg(0,"creating dir %s\n", dir);
912 if (file_mkdir(dir,0)) {
913 dbg(0,"failed creating dir %s\n", dir);
922 * navit_get_destination_file
924 * returns the name of the file used to store destinations with its
927 * arg: gboolean create: create the directory where the file is stored
928 * if it does not exist
931 navit_get_destination_file(gboolean create)
933 return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
937 * navit_get_bookmark_file
939 * returns the name of the file used to store bookmarks with its
942 * arg: gboolean create: create the directory where the file is stored
943 * if it does not exist
946 navit_get_bookmark_file(gboolean create)
948 return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
953 * navit_get_bookmark_file
955 * returns the name of the file used to store the center file with its
958 * arg: gboolean create: create the directory where the file is stored
959 * if it does not exist
962 navit_get_center_file(gboolean create)
964 return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
968 navit_set_center_from_file(struct navit *this_, char *file)
970 #ifndef HAVE_API_ANDROID
974 size_t line_size = 0;
976 struct coord *center;
978 f = fopen(file, "r");
981 getline(&line, &line_size, f);
984 center = transform_center(this_->trans);
985 pro = transform_get_projection(this_->trans);
986 coord_parse(g_strchomp(line), pro, center);
994 navit_write_center_to_file(struct navit *this_, char *file)
998 struct coord *center;
1000 f = fopen(file, "w+");
1002 center = transform_center(this_->trans);
1003 pro = transform_get_projection(this_->trans);
1004 coord_print(pro, center, f);
1014 * Start the route computing to a given set of coordinates
1016 * @param navit The navit instance
1017 * @param c The coordinate to start routing to
1018 * @param description A label which allows the user to later identify this destination in the former destinations selection
1022 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1025 this_->destination=*c;
1026 this_->destination_valid=1;
1028 this_->destination_valid=0;
1029 char *destination_file = navit_get_destination_file(TRUE);
1030 navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
1031 g_free(destination_file);
1032 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1034 route_set_destination(this_->route, c, async);
1036 if (this_->ready == 3)
1042 * @brief Checks if a route is calculated
1044 * This function checks if a route is calculated.
1046 * @param this_ The navit struct whose route should be checked.
1047 * @return True if the route is set, false otherwise.
1050 navit_check_route(struct navit *this_)
1053 return route_get_path_set(this_->route);
1060 * Record the given set of coordinates as a bookmark
1062 * @param navit The navit instance
1063 * @param c The coordinate to store
1064 * @param description A label which allows the user to later identify this bookmark
1068 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
1070 char *bookmark_file = navit_get_bookmark_file(TRUE);
1071 navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
1072 g_free(bookmark_file);
1074 callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
1077 struct navit *global_navit;
1080 navit_add_bookmarks_from_file(struct navit *this_)
1082 char *bookmark_file = navit_get_bookmark_file(FALSE);
1083 struct attr parent={attr_navit, .u.navit=this_};
1084 struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
1085 struct attr *attrs[]={&type, &data, NULL};
1087 this_->bookmark=map_new(&parent, attrs);
1088 g_free(bookmark_file);
1092 navit_former_destinations_active(struct navit *this_)
1094 char *destination_file = navit_get_destination_file(FALSE);
1098 f=fopen(destination_file,"r");
1100 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
1104 g_free(destination_file);
1109 navit_add_former_destinations_from_file(struct navit *this_)
1111 char *destination_file = navit_get_destination_file(FALSE);
1112 struct attr parent={attr_navit, .u.navit=this_};
1113 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
1114 struct attr *attrs[]={&type, &data, NULL};
1115 struct map_rect *mr;
1121 this_->former_destination=map_new(&parent, attrs);
1122 g_free(destination_file);
1123 if (!this_->route || !navit_former_destinations_active(this_))
1125 mr=map_rect_new(this_->former_destination, NULL);
1126 while ((item=map_rect_get_item(mr))) {
1127 if (item->type == type_former_destination && item_coord_get(item, &c, 1))
1130 map_rect_destroy(mr);
1131 pc.pro=map_projection(this_->former_destination);
1135 route_set_destination(this_->route, &pc, 1);
1136 this_->destination=pc;
1137 this_->destination_valid=1;
1143 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1148 if (this_->textfile_debug_log && this_->vehicle) {
1149 str1=g_strdup_vprintf(fmt, ap);
1150 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1151 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1159 navit_speech_estimate(struct navit *this_, char *str)
1161 return speech_estimate_duration(this_->speech, str);
1165 navit_say(struct navit *this_, char *text)
1167 speech_say(this_->speech, text);
1171 * @brief Toggles the navigation announcer for navit
1172 * @param this_ The navit object
1175 navit_cmd_announcer_toggle(struct navit *this_)
1177 struct attr attr, speechattr;
1179 // search for the speech attribute
1180 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1182 // find out if the corresponding attribute attr_active has been set
1183 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1185 attr.u.num = !attr.u.num;
1187 // otherwise disable it because voice is enabled by default
1188 attr.type = attr_active;
1192 // apply the new state
1193 if(!speech_set_attr(speechattr.u.speech, &attr))
1196 // announce that the speech attribute has changed
1197 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1201 navit_speak(struct navit *this_)
1203 struct navigation *nav=this_->navigation;
1204 struct map *map=NULL;
1205 struct map_rect *mr=NULL;
1209 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1211 dbg(1, "this_.speech->active %i\n", attr.u.num);
1216 map=navigation_get_map(nav);
1218 mr=map_rect_new(map, NULL);
1220 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1221 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1222 speech_say(this_->speech, attr.u.str);
1223 navit_add_message(this_, attr.u.str);
1224 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1226 map_rect_destroy(mr);
1231 navit_window_roadbook_update(struct navit *this_)
1233 struct navigation *nav=this_->navigation;
1234 struct map *map=NULL;
1235 struct map_rect *mr=NULL;
1238 struct param_list param[5];
1242 datawindow_mode(this_->roadbook_window, 1);
1244 map=navigation_get_map(nav);
1246 mr=map_rect_new(map, NULL);
1247 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1249 dbg(0,"while loop\n");
1250 while ((item=map_rect_get_item(mr))) {
1251 dbg(0,"item=%p\n", item);
1253 if (item->type != type_nav_position) {
1254 item_attr_get(item, attr_navigation_long, &attr);
1255 if (attr.u.str == NULL) {
1258 dbg(2, "Command='%s'\n", attr.u.str);
1259 param[0].value=g_strdup(attr.u.str);
1261 param[0].value=_("Position");
1262 param[0].name=_("Command");
1264 item_attr_get(item, attr_length, &attr);
1265 dbg(2, "Length=%d\n", attr.u.num);
1266 param[1].name=_("Length");
1268 if ( attr.u.num >= 2000 )
1270 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1274 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1277 item_attr_get(item, attr_time, &attr);
1278 dbg(2, "Time=%d\n", attr.u.num);
1280 param[2].name=_("Time");
1283 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1287 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1290 item_attr_get(item, attr_destination_length, &attr);
1291 dbg(2, "Destlength=%d\n", attr.u.num);
1292 param[3].name=_("Destination Length");
1293 if ( attr.u.num >= 2000 )
1295 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1299 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1302 item_attr_get(item, attr_destination_time, &attr);
1303 dbg(2, "Desttime=%d\n", attr.u.num);
1305 param[4].name=_("Destination Time");
1308 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1312 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1314 datawindow_add(this_->roadbook_window, param, 5);
1316 map_rect_destroy(mr);
1318 datawindow_mode(this_->roadbook_window, 0);
1322 navit_window_roadbook_destroy(struct navit *this_)
1325 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1326 this_->roadbook_window=NULL;
1327 this_->roadbook_callback=NULL;
1330 navit_window_roadbook_new(struct navit *this_)
1332 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1336 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1337 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1338 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1339 navit_window_roadbook_update(this_);
1343 navit_init(struct navit *this_)
1349 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1352 navit_destroy(this_);
1356 dbg(0,"no graphics\n");
1357 navit_destroy(this_);
1360 dbg(2,"Connecting gui to graphics\n");
1361 if (gui_set_graphics(this_->gui, this_->gra)) {
1362 struct attr attr_type_gui, attr_type_graphics;
1363 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1364 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1365 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1366 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1367 dbg(0," for explanations and solutions\n");
1369 navit_destroy(this_);
1372 dbg(2,"Initializing graphics\n");
1373 dbg(2,"Setting Vehicle\n");
1374 navit_set_vehicle(this_, this_->vehicle);
1375 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1376 if (this_->mapsets) {
1377 ms=this_->mapsets->data;
1379 if ((map=route_get_map(this_->route)))
1380 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1381 if ((map=route_get_graph_map(this_->route))) {
1382 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1383 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1385 route_set_mapset(this_->route, ms);
1386 route_set_projection(this_->route, transform_get_projection(this_->trans));
1388 if (this_->tracking) {
1389 tracking_set_mapset(this_->tracking, ms);
1391 tracking_set_route(this_->tracking, this_->route);
1393 if (this_->navigation) {
1394 if ((map=navigation_get_map(this_->navigation))) {
1395 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1396 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1399 if (this_->tracking) {
1400 if ((map=tracking_get_map(this_->tracking))) {
1401 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1402 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1405 navit_add_bookmarks_from_file(this_);
1406 navit_add_former_destinations_from_file(this_);
1409 struct attr callback;
1410 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1411 callback.type=attr_callback;
1412 callback.u.callback=this_->route_cb;
1413 route_add_attr(this_->route, &callback);
1415 if (this_->navigation) {
1416 if (this_->speech) {
1417 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1418 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1421 navigation_set_route(this_->navigation, this_->route);
1423 dbg(2,"Setting Center\n");
1424 char *center_file = navit_get_center_file(FALSE);
1425 navit_set_center_from_file(this_, center_file);
1426 g_free(center_file);
1428 if (this_->menubar) {
1429 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1431 navit_add_menu_windows_items(this_, men);
1437 navit_window_roadbook_new(this_);
1438 navit_window_items_new(this_);
1441 messagelist_init(this_->messages);
1443 navit_set_cursors(this_);
1445 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1446 callback=(this_->ready == 2);
1448 dbg(2,"ready=%d\n",this_->ready);
1449 if (this_->ready == 3)
1452 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1454 routech_test(this_);
1459 navit_zoom_to_route(struct navit *this_, int orientation)
1462 struct map_rect *mr=NULL;
1465 struct coord_rect r;
1466 int count=0,scale=16;
1470 map=route_get_map(this_->route);
1471 dbg(1,"map=%p\n",map);
1473 mr=map_rect_new(map, NULL);
1474 dbg(1,"mr=%p\n",mr);
1476 while ((item=map_rect_get_item(mr))) {
1477 dbg(1,"item=%s\n", item_to_name(item->type));
1478 while (item_coord_get(item, &c, 1)) {
1483 coord_rect_extend(&r, &c);
1490 c.x=(r.rl.x+r.lu.x)/2;
1491 c.y=(r.rl.y+r.lu.y)/2;
1492 dbg(1,"count=%d\n",count);
1493 if (orientation != -1)
1494 transform_set_yaw(this_->trans, orientation);
1495 transform_set_center(this_->trans, &c);
1496 dbg(1,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1497 while (scale < 1<<20) {
1499 transform_set_scale(this_->trans, scale);
1500 transform_setup_source_rect(this_->trans);
1501 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1502 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1503 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1504 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1505 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1511 if (this_->ready == 3)
1512 navit_draw_async(this_,0);
1516 navit_cmd_zoom_to_route(struct navit *this)
1518 navit_zoom_to_route(this, 0);
1523 * Change the current zoom level
1525 * @param navit The navit instance
1526 * @param center The point where to center the map, including its projection
1530 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1532 struct coord *c=transform_center(this_->trans);
1534 enum projection pro = transform_get_projection(this_->trans);
1535 if (pro != center->pro) {
1538 transform_from_to(&c1, center->pro, &c2, pro);
1545 navit_set_timeout(this_);
1546 if (this_->ready == 3)
1551 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1555 transform_set_center(this_->trans, c);
1556 transform_get_size(this_->trans, &width, &height);
1559 update_transformation(this_->trans, &po, p, NULL);
1561 navit_set_timeout(this_);
1565 * Links all vehicles to a cursor depending on the current profile.
1567 * @param this_ A navit instance
1568 * @author Ralph Sennhauser (10/2009)
1571 navit_set_cursors(struct navit *this_)
1574 struct navit_vehicle *nv;
1578 v=g_list_first(this_->vehicles); // GList of navit_vehicles
1581 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL))
1582 c=layout_get_cursor(this_->layout_current, name.u.str);
1584 c=layout_get_cursor(this_->layout_current, "default");
1585 vehicle_set_cursor(nv->vehicle, c);
1592 navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir)
1595 struct navit_vehicle *nv=this_->vehicle;
1597 float offset=30; // Cursor offset from the center of the screen (percent).
1598 #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 */
1599 float min_offset = 0.; // Percent offset at min_offset_speed.
1600 float max_offset = 30.; // Percent offset at max_offset_speed.
1601 int min_offset_speed = 2; // Speed in km/h
1602 int max_offset_speed = 50; // Speed ini km/h
1603 // Calculate cursor offset from the center of the screen, upon speed.
1604 if (nv->speed <= min_offset_speed) {
1605 offset = min_offset;
1606 } else if (nv->speed > max_offset_speed) {
1607 offset = max_offset;
1609 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1613 transform_get_size(this_->trans, &width, &height);
1614 if (this_->orientation == -1) {
1616 p->y=(50 + offset)*height/100;
1621 if (this_->tracking && this_->tracking_flag) {
1622 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1624 mdir=nv->dir-this_->orientation;
1627 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1628 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1630 *dir=this_->orientation;
1636 navit_set_center_cursor(struct navit *this_)
1640 struct navit_vehicle *nv=this_->vehicle;
1641 navit_get_cursor_pnt(this_, &pn, &dir);
1642 transform_set_yaw(this_->trans, dir);
1643 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1644 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1645 if (this_->ready == 3)
1646 navit_draw_async(this_, 1);
1650 navit_cmd_set_center_cursor(struct navit *this_)
1652 navit_set_center_cursor(this_);
1656 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1660 transform_reverse(this_->trans, p, &c);
1663 pc.pro = transform_get_projection(this_->trans);
1664 navit_set_center(this_, &pc, set_timeout);
1668 switch((*attrs)->type) {
1670 zoom=(*attrs)->u.num;
1673 g=*((*attrs)->u.coord_geo);
1678 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1680 int dir=0, orient_old=0, attr_updated=0;
1684 struct navit_vehicle *nv;
1685 struct attr active=(struct attr){attr_active,{(void *)0}};
1688 switch (attr->type) {
1690 attr_updated=(this_->autozoom_secs != attr->u.num);
1691 this_->autozoom_secs = attr->u.num;
1693 case attr_autozoom_active:
1694 attr_updated=(this_->autozoom_active != attr->u.num);
1695 this_->autozoom_active = attr->u.num;
1698 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1699 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1700 transform_set_center(this_->trans, &co);
1702 case attr_drag_bitmap:
1703 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1704 this_->drag_bitmap=!!attr->u.num;
1706 case attr_flags_graphics:
1707 attr_updated=(this_->graphics_flags != attr->u.num);
1708 this_->graphics_flags=attr->u.num;
1711 if (!this_->vehicle)
1713 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1714 this_->vehicle->follow_curr = attr->u.num;
1717 if(this_->layout_current!=attr->u.layout) {
1718 this_->layout_current=attr->u.layout;
1719 graphics_font_destroy_all(this_->gra);
1720 navit_set_cursors(this_);
1721 if (this_->ready == 3)
1726 case attr_layout_name:
1730 if (!strcmp(lay->name,attr->u.str)) {
1732 attr.type=attr_layout;
1734 return navit_set_attr_do(this_, &attr, init);
1739 case attr_orientation:
1740 orient_old=this_->orientation;
1741 this_->orientation=attr->u.num;
1743 if (this_->orientation != -1) {
1744 dir = this_->orientation;
1746 if (this_->vehicle) {
1747 dir = this_->vehicle->dir;
1750 transform_set_yaw(this_->trans, dir);
1751 if (orient_old != this_->orientation) {
1753 if (this_->ready == 3)
1760 case attr_osd_configuration:
1761 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1762 attr_updated=(this_->osd_configuration != attr->u.num);
1763 this_->osd_configuration=attr->u.num;
1766 attr_updated=(this_->pitch != attr->u.num);
1767 this_->pitch=attr->u.num;
1768 transform_set_pitch(this_->trans, this_->pitch);
1769 if (!init && attr_updated && this_->ready == 3)
1772 case attr_projection:
1773 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1774 navit_projection_set(this_, attr->u.projection);
1778 case attr_recent_dest:
1779 attr_updated=(this_->recentdest_count != attr->u.num);
1780 this_->recentdest_count=attr->u.num;
1783 if(this_->speech && this_->speech != attr->u.speech) {
1785 this_->speech = attr->u.speech;
1789 attr_updated=(this_->center_timeout != attr->u.num);
1790 this_->center_timeout = attr->u.num;
1793 attr_updated=(this_->tracking_flag != !!attr->u.num);
1794 this_->tracking_flag=!!attr->u.num;
1796 case attr_use_mousewheel:
1797 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1798 this_->use_mousewheel=!!attr->u.num;
1804 if (nv->vehicle == attr->u.vehicle) {
1805 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1807 vehicle_set_attr(this_->vehicle->vehicle, &active);
1809 vehicle_set_attr(nv->vehicle, &active);
1812 navit_set_vehicle(this_, nv);
1818 zoom=transform_get_scale(this_->trans);
1819 attr_updated=(zoom != attr->u.num);
1820 transform_set_scale(this_->trans, attr->u.num);
1821 if (attr_updated && !init)
1825 attr_updated=(attr->u.num != this_->zoom_min);
1826 this_->zoom_min=attr->u.num;
1829 attr_updated=(attr->u.num != this_->zoom_max);
1830 this_->zoom_max=attr->u.num;
1833 navit_add_message(this_, attr->u.str);
1835 case attr_follow_cursor:
1836 attr_updated=(this_->follow_cursor != !!attr->u.num);
1837 this_->follow_cursor=!!attr->u.num;
1842 if (attr_updated && !init) {
1843 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1844 if (attr->type == attr_osd_configuration)
1845 graphics_draw_mode(this_->gra, draw_mode_end);
1851 navit_set_attr(struct navit *this_, struct attr *attr)
1853 return navit_set_attr_do(this_, attr, 0);
1857 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1859 struct message *msg;
1865 msg = navit_get_messages(this_);
1873 len += strlen(msg->text) + 1;
1876 attr->u.str = g_malloc(len + 1);
1878 msg = navit_get_messages(this_);
1881 g_stpcpy((attr->u.str + offset), msg->text);
1882 offset += strlen(msg->text);
1883 attr->u.str[offset] = '\n';
1889 attr->u.str[len] = '\0';
1891 case attr_bookmark_map:
1892 attr->u.map=this_->bookmark;
1894 case attr_callback_list:
1895 attr->u.callback_list=this_->attr_cbl;
1897 case attr_destination:
1898 if (! this_->destination_valid)
1900 attr->u.pcoord=&this_->destination;
1902 case attr_displaylist:
1903 attr->u.displaylist=this_->displaylist;
1904 return (attr->u.displaylist != NULL);
1906 if (!this_->vehicle)
1908 attr->u.num=this_->vehicle->follow_curr;
1910 case attr_former_destination_map:
1911 attr->u.map=this_->former_destination;
1914 attr->u.graphics=this_->gra;
1915 ret=(attr->u.graphics != NULL);
1918 attr->u.gui=this_->gui;
1919 ret=(attr->u.gui != NULL);
1924 iter->u.list=g_list_next(iter->u.list);
1926 iter->u.list=this_->layouts;
1930 attr->u.layout=(struct layout *)iter->u.list->data;
1932 attr->u.layout=this_->layout_current;
1936 if (iter && this_->mapsets) {
1937 if (!iter->u.mapset_handle) {
1938 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1940 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1942 mapset_close(iter->u.mapset_handle);
1950 attr->u.mapset=this_->mapsets->data;
1951 ret=(attr->u.mapset != NULL);
1953 case attr_navigation:
1954 attr->u.navigation=this_->navigation;
1956 case attr_orientation:
1957 attr->u.num=this_->orientation;
1959 case attr_osd_configuration:
1960 attr->u.num=this_->osd_configuration;
1963 attr->u.num=transform_get_pitch(this_->trans);
1965 case attr_projection:
1967 attr->u.num=transform_get_projection(this_->trans);
1973 attr->u.route=this_->route;
1976 attr->u.speech=this_->speech;
1979 attr->u.num=this_->tracking_flag;
1981 case attr_transformation:
1982 attr->u.transformation=this_->trans;
1987 iter->u.list=g_list_next(iter->u.list);
1989 iter->u.list=this_->vehicles;
1993 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1995 if(this_->vehicle) {
1996 attr->u.vehicle=this_->vehicle->vehicle;
2003 attr->u.num=transform_get_scale(this_->trans);
2005 case attr_autozoom_active:
2006 attr->u.num=this_->autozoom_active;
2008 case attr_follow_cursor:
2009 attr->u.num=this_->follow_cursor;
2019 navit_add_log(struct navit *this_, struct log *log)
2021 struct attr type_attr;
2022 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2024 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2025 char *header = "type=track_tracked\n";
2026 if (this_->textfile_debug_log)
2028 log_set_header(log, header, strlen(header));
2029 this_->textfile_debug_log=log;
2036 navit_add_attr(struct navit *this_, struct attr *attr)
2039 switch (attr->type) {
2041 navit_add_callback(this_, attr->u.callback);
2044 ret=navit_add_log(this_, attr->u.log);
2047 ret=navit_set_gui(this_, attr->u.gui);
2050 ret=navit_set_graphics(this_, attr->u.graphics);
2053 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2054 if(!this_->layout_current)
2055 this_->layout_current=attr->u.layout;
2058 this_->route=attr->u.route;
2061 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2063 case attr_navigation:
2064 this_->navigation=attr->u.navigation;
2066 case attr_recent_dest:
2067 this_->recentdest_count = attr->u.num;
2070 this_->speech=attr->u.speech;
2073 this_->tracking=attr->u.tracking;
2076 ret=navit_add_vehicle(this_, attr->u.vehicle);
2078 case attr_vehicleprofile:
2079 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2081 case attr_autozoom_min:
2082 this_->autozoom_min = attr->u.num;
2087 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2092 navit_remove_attr(struct navit *this_, struct attr *attr)
2095 switch (attr->type) {
2097 navit_remove_callback(this_, attr->u.callback);
2106 navit_attr_iter_new(void)
2108 return g_new0(struct attr_iter, 1);
2112 navit_attr_iter_destroy(struct attr_iter *iter)
2118 navit_add_callback(struct navit *this_, struct callback *cb)
2120 callback_list_add(this_->attr_cbl, cb);
2124 navit_remove_callback(struct navit *this_, struct callback *cb)
2126 callback_list_remove(this_->attr_cbl, cb);
2130 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2132 * @param navit The navit instance
2137 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2139 struct point cursor_pnt;
2140 enum projection pro;
2147 pro=transform_get_projection(this_->trans);
2148 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2150 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
2155 pro=transform_get_projection(this_->trans);
2156 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2159 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2161 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2167 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2169 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2170 struct pcoord cursor_pc;
2171 struct point cursor_pnt, *pnt=&cursor_pnt;
2172 struct tracking *tracking=NULL;
2173 enum projection pro=transform_get_projection(this_->trans);
2175 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2179 if (this_->ready != 3) {
2180 profile(0,"return 1\n");
2183 navit_layout_switch(this_);
2184 if (this_->vehicle == nv && this_->tracking_flag)
2185 tracking=this_->tracking;
2187 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2188 attr_object=tracking;
2189 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2191 attr_object=nv->vehicle;
2192 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2194 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2195 if (!attr_valid.u.num != attr_position_valid_invalid)
2197 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2198 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2199 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2200 profile(0,"return 2\n");
2203 nv->dir=*attr_dir.u.numd;
2204 nv->speed=*attr_speed.u.numd;
2205 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2206 if (nv != this_->vehicle) {
2207 navit_vehicle_draw(this_, nv, NULL);
2208 profile(0,"return 3\n");
2211 cursor_pc.x = nv->coord.x;
2212 cursor_pc.y = nv->coord.y;
2213 cursor_pc.pro = pro;
2216 route_set_position_from_tracking(this_->route, tracking, pro);
2218 route_set_position(this_->route, &cursor_pc);
2220 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2221 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2222 if (this_->gui && nv->speed > 2)
2223 gui_disable_suspend(this_->gui);
2225 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2226 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2227 (nv->follow_curr == 1 || !transform_within_border(this_->trans, &cursor_pnt, border)))
2228 navit_set_center_cursor(this_);
2230 navit_vehicle_draw(this_, nv, pnt);
2232 if (nv->follow_curr > 1)
2235 nv->follow_curr=nv->follow;
2236 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2238 /* Finally, if we reached our destination, stop navigation. */
2239 if (this_->route && route_destination_reached(this_->route)) {
2240 navit_set_destination(this_, NULL, NULL, 0);
2242 profile(0,"return 5\n");
2246 * Set the position of the vehicle
2248 * @param navit The navit instance
2249 * @param c The coordinate to set as position
2254 navit_set_position(struct navit *this_, struct pcoord *c)
2257 route_set_position(this_->route, c);
2258 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2260 if (this_->ready == 3)
2265 navit_set_vehicleprofile(struct navit *this_, char *name)
2269 l=this_->vehicleprofiles;
2271 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2272 if (!strcmp(attr.u.str, name)) {
2273 this_->vehicleprofile=l->data;
2275 route_set_profile(this_->route, this_->vehicleprofile);
2285 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2289 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2290 if (navit_set_vehicleprofile(this_, attr.u.str))
2293 navit_set_vehicleprofile(this_,"car");
2297 * Register a new vehicle
2299 * @param navit The navit instance
2300 * @param v The vehicle instance
2301 * @returns 1 for success
2304 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2306 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2307 struct attr follow, active, animate;
2312 nv->animate_cursor=0;
2313 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2314 nv->follow=nv->follow=follow.u.num;
2315 nv->follow_curr=nv->follow;
2316 this_->vehicles=g_list_append(this_->vehicles, nv);
2317 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2318 navit_set_vehicle(this_, nv);
2319 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2320 nv->animate_cursor=animate.u.num;
2321 nv->callback.type=attr_callback;
2322 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2323 vehicle_add_attr(nv->vehicle, &nv->callback);
2324 vehicle_set_attr(nv->vehicle, &this_->self);
2332 navit_get_gui(struct navit *this_)
2337 struct transformation *
2338 navit_get_trans(struct navit *this_)
2340 return this_->trans;
2344 navit_get_route(struct navit *this_)
2346 return this_->route;
2350 navit_get_navigation(struct navit *this_)
2352 return this_->navigation;
2355 struct displaylist *
2356 navit_get_displaylist(struct navit *this_)
2358 return this_->displaylist;
2362 navit_layout_switch(struct navit *n)
2366 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2367 double trise,tset,trise_actual;
2369 int year, month, day;
2371 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2372 return; //No layout - nothing to switch
2376 l=layout_attr.u.layout;
2378 if (l->dayname || l->nightname) {
2379 //Ok, we know that we have profile to switch
2381 //Check that we aren't calculating too fast
2382 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2383 currTs=iso8601_to_secs(iso8601_attr.u.str);
2384 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2386 if (currTs-(n->prevTs)<60) {
2387 //We've have to wait a little
2390 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2392 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2393 //No position - no sun
2396 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2397 return; //No valid fix yet
2400 //We calculate sunrise anyway, cause it is needed both for day and for night
2401 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,35,1,&trise,&tset)!=0) {
2402 //near the pole sun never rises/sets, so we should never switch profiles
2408 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2411 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2412 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2413 //The sun is rising now!
2414 if (strcmp(l->name,l->dayname)) {
2415 navit_set_layout_by_name(n,l->dayname);
2420 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,-24,0,&trise,&tset)!=0) {
2421 //near the pole sun never rises/sets, so we should never switch profiles
2425 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2426 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2427 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
2428 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2430 if (strcmp(l->name,l->nightname)) {
2431 navit_set_layout_by_name(n,l->nightname);
2441 navit_set_layout_by_name(struct navit *n,char *name)
2444 struct attr_iter iter;
2445 struct attr layout_attr;
2449 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2450 return 0; //No layouts - nothing to do
2452 if (iter.u.list==NULL) {
2456 iter.u.list=g_list_first(iter.u.list);
2458 while(iter.u.list) {
2459 l=(struct layout*)iter.u.list->data;
2460 if (!strcmp(name,l->name)) {
2461 layout_attr.u.layout=l;
2462 layout_attr.type=attr_layout;
2463 navit_set_attr(n,&layout_attr);
2464 iter.u.list=g_list_first(iter.u.list);
2467 iter.u.list=g_list_next(iter.u.list);
2470 iter.u.list=g_list_first(iter.u.list);
2475 navit_block(struct navit *this_, int block)
2478 this_->blocked |= 1;
2479 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2480 this_->blocked |= 2;
2483 if (this_->blocked & 2) {
2493 navit_destroy(struct navit *this_)
2495 /* TODO: destroy objects contained in this_ */
2497 vehicle_destroy(this_->vehicle->vehicle);
2498 char *center_file = navit_get_center_file(TRUE);
2499 navit_write_center_to_file(this_, center_file);
2500 g_free(center_file);
2501 callback_destroy(this_->nav_speech_cb);
2502 callback_destroy(this_->roadbook_callback);
2503 callback_destroy(this_->popup_callback);
2504 callback_destroy(this_->motion_timeout_callback);
2506 graphics_remove_callback(this_->gra, this_->resize_callback);
2507 callback_destroy(this_->resize_callback);
2509 graphics_remove_callback(this_->gra, this_->button_callback);
2510 callback_destroy(this_->button_callback);
2512 graphics_remove_callback(this_->gra, this_->motion_callback);
2513 callback_destroy(this_->motion_callback);