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"
64 #include "bookmarks.h"
67 * @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
71 //! The navit_vehicule
72 struct navit_vehicle {
74 /*! Limit of the follow counter. See navit_add_vehicle */
76 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
80 struct coord last; /*< Position of the last update of this vehicle */
81 struct vehicle *vehicle;
91 struct layout *layout_current;
93 struct action *action;
94 struct transformation *trans;
95 struct compass *compass;
97 struct navigation *navigation;
98 struct speech *speech;
99 struct tracking *tracking;
102 struct displaylist *displaylist;
105 int recentdest_count;
106 int osd_configuration;
108 GList *windows_items;
109 struct navit_vehicle *vehicle;
110 struct callback_list *attr_cbl;
111 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb;
112 struct datawindow *roadbook_window;
113 struct map *former_destination;
114 struct point pressed, last, current;
115 int button_pressed,moved,popped,zoomed;
120 struct event_timeout *button_timeout, *motion_timeout;
121 struct callback *motion_timeout_callback;
123 int ignore_graphics_events;
124 struct log *textfile_debug_log;
125 struct pcoord destination;
126 int destination_valid;
131 struct messagelist *messages;
132 struct callback *resize_callback,*button_callback,*motion_callback;
133 struct vehicleprofile *vehicleprofile;
134 GList *vehicleprofiles;
139 int zoom_min, zoom_max;
141 struct bookmarks *bookmarks;
144 struct gui *main_loop_gui;
149 struct mapset_handle *mapset_handle;
153 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
154 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
155 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
156 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
157 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir);
158 static void navit_set_cursors(struct navit *this_);
159 static void navit_cmd_zoom_to_route(struct navit *this);
160 static void navit_cmd_set_center_cursor(struct navit *this_);
161 static void navit_cmd_announcer_toggle(struct navit *this_);
162 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
164 struct navit *global_navit;
167 navit_add_mapset(struct navit *this_, struct mapset *ms)
169 this_->mapsets = g_list_append(this_->mapsets, ms);
173 navit_get_mapset(struct navit *this_)
176 return this_->mapsets->data;
178 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
184 navit_get_tracking(struct navit *this_)
186 return this_->tracking;
190 navit_draw_async(struct navit *this_, int async)
193 struct navit_vehicle *nv;
195 if (this_->blocked) {
199 transform_setup_source_rect(this_->trans);
203 navit_vehicle_draw(this_, nv, NULL);
206 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
210 navit_draw(struct navit *this_)
212 if (this_->ready == 3)
213 navit_draw_async(this_, 0);
217 navit_get_ready(struct navit *this_)
225 navit_draw_displaylist(struct navit *this_)
227 if (this_->ready == 3)
228 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
232 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
235 if (attr->type != attr_route_status)
238 if (this_->ready != 3)
240 if (updated != route_status_path_done_new)
242 if (this_->vehicle) {
243 if (this_->vehicle->follow_curr == 1)
245 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
246 this_->vehicle->follow_curr=this_->vehicle->follow;
252 navit_handle_resize(struct navit *this_, int w, int h)
254 struct map_selection sel;
255 int callback=(this_->ready == 1);
257 if (this_->w != w || this_->h != h) {
258 memset(&sel, 0, sizeof(sel));
263 transform_set_screen_selection(this_->trans, &sel);
264 graphics_init(this_->gra);
265 graphics_set_rect(this_->gra, &sel.u.p_rect);
268 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
269 if (this_->ready == 3)
274 navit_resize(void *data, int w, int h)
276 struct navit *this=data;
277 if (!this->ignore_graphics_events)
278 navit_handle_resize(this, w, h);
282 navit_get_width(struct navit *this_)
289 navit_get_height(struct navit *this_)
295 navit_popup(void *data)
297 struct navit *this_=data;
298 popup(this_, 1, &this_->pressed);
299 this_->button_timeout=NULL;
305 navit_ignore_button(struct navit *this_)
307 if (this_->ignore_button)
309 this_->ignore_button=1;
314 navit_ignore_graphics_events(struct navit *this_, int ignore)
316 this_->ignore_graphics_events=ignore;
320 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
325 double angleo,anglen;
327 if (!transform_reverse(tr, old, &co))
330 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
331 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
332 yaw=transform_get_yaw(tr)+angleo-anglen;
333 transform_set_yaw(tr, yaw % 360);
335 if (!transform_reverse(tr, new, &cn))
337 cp=transform_get_center(tr);
340 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
341 transform_set_center(tr, &c);
345 navit_set_timeout(struct navit *this_)
348 follow.type=attr_follow;
349 follow.u.num=this_->center_timeout;
350 navit_set_attr(this_, &follow);
354 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
358 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
359 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
360 if (this_->ignore_button) {
361 this_->ignore_button=0;
369 this_->button_pressed=1;
373 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
376 navit_set_center_screen(this_, p, 1);
378 popup(this_, button, p);
379 if (button == 4 && this_->use_mousewheel) {
381 navit_zoom_in(this_, 2, p);
383 if (button == 5 && this_->use_mousewheel) {
385 navit_zoom_out(this_, 2, p);
389 this_->button_pressed=0;
390 if (this_->button_timeout) {
391 event_remove_timeout(this_->button_timeout);
392 this_->button_timeout=NULL;
393 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
394 navit_set_center_screen(this_, p, !this_->zoomed);
397 if (this_->motion_timeout) {
398 event_remove_timeout(this_->motion_timeout);
399 this_->motion_timeout=NULL;
406 update_transformation(this_->trans, &this_->pressed, p, &pr);
408 update_transformation(this_->trans, &this_->pressed, p, NULL);
410 graphics_draw_drag(this_->gra, NULL);
411 graphics_overlay_disable(this_->gra, 0);
413 navit_set_timeout(this_);
422 navit_button(void *data, int pressed, int button, struct point *p)
424 struct navit *this=data;
425 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
426 if (!this->ignore_graphics_events) {
427 if (! this->popup_callback)
428 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
429 navit_handle_button(this, pressed, button, p, this->popup_callback);
435 navit_motion_timeout(struct navit *this_)
439 if (this_->drag_bitmap) {
441 point.x=(this_->current.x-this_->pressed.x);
442 point.y=(this_->current.y-this_->pressed.y);
443 if (graphics_draw_drag(this_->gra, &point)) {
444 graphics_overlay_disable(this_->gra, 1);
445 graphics_draw_mode(this_->gra, draw_mode_end);
447 this_->motion_timeout=NULL;
451 dx=(this_->current.x-this_->last.x);
452 dy=(this_->current.y-this_->last.y);
454 struct transformation *tr;
456 this_->last=this_->current;
457 graphics_overlay_disable(this_->gra, 1);
458 tr=transform_dup(this_->trans);
462 update_transformation(tr, &this_->pressed, &this_->current, &pr);
464 update_transformation(tr, &this_->pressed, &this_->current, NULL);
467 graphics_displaylist_move(this_->displaylist, dx, dy);
469 graphics_draw_cancel(this_->gra, this_->displaylist);
470 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
471 transform_destroy(tr);
474 this_->motion_timeout=NULL;
479 navit_handle_motion(struct navit *this_, struct point *p)
483 if (this_->button_pressed && !this_->popped) {
484 dx=(p->x-this_->pressed.x);
485 dy=(p->y-this_->pressed.y);
486 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
488 if (this_->button_timeout) {
489 event_remove_timeout(this_->button_timeout);
490 this_->button_timeout=NULL;
493 if (! this_->motion_timeout_callback)
494 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
495 if (! this_->motion_timeout)
496 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
502 navit_motion(void *data, struct point *p)
504 struct navit *this=data;
505 if (!this->ignore_graphics_events)
506 navit_handle_motion(this, p);
510 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
512 struct coord c1, c2, *center;
513 if (scale < this_->zoom_min)
514 scale=this_->zoom_min;
515 if (scale > this_->zoom_max)
516 scale=this_->zoom_max;
518 transform_reverse(this_->trans, p, &c1);
519 transform_set_scale(this_->trans, scale);
521 transform_reverse(this_->trans, p, &c2);
522 center = transform_center(this_->trans);
523 center->x += c1.x - c2.x;
524 center->y += c1.y - c2.y;
531 * @brief Automatically adjusts zoom level
533 * This function automatically adjusts the current
534 * zoom level according to the current speed.
536 * @param this_ The navit struct
537 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
538 * @param speed The vehicles speed in meters per second
539 * @param dir The direction into which the vehicle moves
542 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
549 if (! this_->autozoom_active) {
553 distance = speed * this_->autozoom_secs;
555 transform_get_size(this_->trans, &w, &h);
556 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
557 scale = transform_get_scale(this_->trans);
559 /* We make sure that the point we want to see is within a certain range
560 * around the vehicle. The radius of this circle is the size of the
561 * screen. This doesn't necessarily mean the point is visible because of
562 * perspective etc. Quite rough, but should be enough. */
565 new_scale = (double)distance / h * 16;
567 new_scale = (double)distance / w * 16;
570 if (abs(new_scale - scale) < 2) {
574 if (new_scale >= this_->autozoom_min) {
575 navit_scale(this_, (long)new_scale, &pc, 0);
577 if (scale != this_->autozoom_min) {
578 navit_scale(this_, this_->autozoom_min, &pc, 0);
584 * Change the current zoom level, zooming closer to the ground
586 * @param navit The navit instance
587 * @param factor The zoom factor, usually 2
588 * @param p The invariant point (if set to NULL, default to center)
592 navit_zoom_in(struct navit *this_, int factor, struct point *p)
594 long scale=transform_get_scale(this_->trans)/factor;
597 navit_scale(this_, scale, p, 1);
601 * Change the current zoom level
603 * @param navit The navit instance
604 * @param factor The zoom factor, usually 2
605 * @param p The invariant point (if set to NULL, default to center)
609 navit_zoom_out(struct navit *this_, int factor, struct point *p)
611 long scale=transform_get_scale(this_->trans)*factor;
612 navit_scale(this_, scale, p, 1);
616 navit_cmd_zoom_in(struct navit *this_)
619 if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
620 navit_zoom_in(this_, 2, &p);
621 this_->vehicle->follow_curr=this_->vehicle->follow;
623 navit_zoom_in(this_, 2, NULL);
628 navit_cmd_zoom_out(struct navit *this_)
631 if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
632 navit_zoom_out(this_, 2, &p);
633 this_->vehicle->follow_curr=this_->vehicle->follow;
635 navit_zoom_out(this_, 2, NULL);
641 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
643 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
644 navit_say(this, in[0]->u.str);
648 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
651 char *description=NULL;
656 pc.pro = transform_get_projection(this->trans);
657 if (ATTR_IS_COORD(in[0]->type)) {
658 pc.x=in[0]->u.coord->x;
659 pc.y=in[0]->u.coord->y;
661 } else if (ATTR_IS_PCOORD(in[0]->type)) {
664 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
669 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
675 if (in[0] && ATTR_IS_STRING(in[0]->type))
676 description=in[0]->u.str;
677 navit_set_destination(this, &pc, description, 1);
680 static struct command_table commands[] = {
681 {"zoom_in",command_cast(navit_cmd_zoom_in)},
682 {"zoom_out",command_cast(navit_cmd_zoom_out)},
683 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
684 {"say",command_cast(navit_cmd_say)},
685 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
686 {"set_destination",command_cast(navit_cmd_set_destination)},
687 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
692 navit_new(struct attr *parent, struct attr **attrs)
694 struct navit *this_=g_new0(struct navit, 1);
695 struct pcoord center;
698 enum projection pro=projection_mg;
703 this_->self.type=attr_navit;
704 this_->self.u.navit=this_;
705 this_->attr_cbl=callback_list_new();
707 this_->orientation=-1;
708 this_->tracking_flag=1;
709 this_->recentdest_count=10;
710 this_->osd_configuration=-1;
712 this_->center_timeout = 10;
713 this_->use_mousewheel = 1;
714 this_->autozoom_secs = 10;
715 this_->autozoom_min = 7;
716 this_->autozoom_active = 0;
718 this_->zoom_max = 2097152;
719 this_->follow_cursor = 1;
722 this_->trans = transform_new();
723 transform_from_geo(pro, &g, &co);
728 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
730 this_->bookmarks=bookmarks_new(&this_->self, this_->trans);
734 for (;*attrs; attrs++) {
735 navit_set_attr_do(this_, *attrs, 1);
737 this_->displaylist=graphics_displaylist_new();
738 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
740 this_->messages = messagelist_new(attrs);
746 navit_set_gui(struct navit *this_, struct gui *gui)
751 if (gui_has_main_loop(this_->gui)) {
752 if (! main_loop_gui) {
753 main_loop_gui=this_->gui;
755 dbg(0,"gui with main loop already active, ignoring this instance");
763 navit_add_message(struct navit *this_, char *message)
765 message_new(this_->messages, message);
769 *navit_get_messages(struct navit *this_)
771 return message_get(this_->messages);
775 navit_set_graphics(struct navit *this_, struct graphics *gra)
780 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
781 graphics_add_callback(gra, this_->resize_callback);
782 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
783 graphics_add_callback(gra, this_->button_callback);
784 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
785 graphics_add_callback(gra, this_->motion_callback);
790 navit_get_graphics(struct navit *this_)
795 struct vehicleprofile *
796 navit_get_vehicleprofile(struct navit *this_)
798 return this_->vehicleprofile;
802 navit_get_vehicleprofiles(struct navit *this_)
804 return this_->vehicleprofiles;
808 navit_projection_set(struct navit *this_, enum projection pro, int draw)
813 c=transform_center(this_->trans);
814 transform_to_geo(transform_get_projection(this_->trans), c, &g);
815 transform_set_projection(this_->trans, pro);
816 transform_from_geo(pro, &g, c);
822 * Start the route computing to a given set of coordinates
824 * @param navit The navit instance
825 * @param c The coordinate to start routing to
826 * @param description A label which allows the user to later identify this destination in the former destinations selection
830 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
833 this_->destination=*c;
834 this_->destination_valid=1;
836 this_->destination_valid=0;
837 char *destination_file = bookmarks_get_destination_file(TRUE);
838 bookmarks_append_coord(this_->bookmarks, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
839 g_free(destination_file);
840 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
842 route_set_destination(this_->route, c, async);
844 if (this_->ready == 3)
850 * @brief Checks if a route is calculated
852 * This function checks if a route is calculated.
854 * @param this_ The navit struct whose route should be checked.
855 * @return True if the route is set, false otherwise.
858 navit_check_route(struct navit *this_)
861 return route_get_path_set(this_->route);
868 navit_former_destinations_active(struct navit *this_)
870 char *destination_file = bookmarks_get_destination_file(FALSE);
874 f=fopen(destination_file,"r");
876 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
880 g_free(destination_file);
885 navit_add_former_destinations_from_file(struct navit *this_)
887 char *destination_file = bookmarks_get_destination_file(FALSE);
888 struct attr parent={attr_navit, .u.navit=this_};
889 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
890 struct attr *attrs[]={&type, &data, NULL};
897 this_->former_destination=map_new(&parent, attrs);
898 g_free(destination_file);
899 if (!this_->route || !navit_former_destinations_active(this_))
901 mr=map_rect_new(this_->former_destination, NULL);
902 while ((item=map_rect_get_item(mr))) {
903 if (item->type == type_former_destination && item_coord_get(item, &c, 1))
906 map_rect_destroy(mr);
907 pc.pro=map_projection(this_->former_destination);
911 route_set_destination(this_->route, &pc, 1);
912 this_->destination=pc;
913 this_->destination_valid=1;
919 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
924 if (this_->textfile_debug_log && this_->vehicle) {
925 str1=g_strdup_vprintf(fmt, ap);
926 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
927 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
935 navit_speech_estimate(struct navit *this_, char *str)
937 return speech_estimate_duration(this_->speech, str);
941 navit_say(struct navit *this_, char *text)
943 speech_say(this_->speech, text);
947 * @brief Toggles the navigation announcer for navit
948 * @param this_ The navit object
951 navit_cmd_announcer_toggle(struct navit *this_)
953 struct attr attr, speechattr;
955 // search for the speech attribute
956 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
958 // find out if the corresponding attribute attr_active has been set
959 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
961 attr.u.num = !attr.u.num;
963 // otherwise disable it because voice is enabled by default
964 attr.type = attr_active;
968 // apply the new state
969 if(!speech_set_attr(speechattr.u.speech, &attr))
972 // announce that the speech attribute has changed
973 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
977 navit_speak(struct navit *this_)
979 struct navigation *nav=this_->navigation;
980 struct map *map=NULL;
981 struct map_rect *mr=NULL;
985 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
987 dbg(1, "this_.speech->active %i\n", attr.u.num);
992 map=navigation_get_map(nav);
994 mr=map_rect_new(map, NULL);
996 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
997 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
998 speech_say(this_->speech, attr.u.str);
999 navit_add_message(this_, attr.u.str);
1000 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1002 map_rect_destroy(mr);
1007 navit_window_roadbook_update(struct navit *this_)
1009 struct navigation *nav=this_->navigation;
1010 struct map *map=NULL;
1011 struct map_rect *mr=NULL;
1014 struct param_list param[5];
1018 datawindow_mode(this_->roadbook_window, 1);
1020 map=navigation_get_map(nav);
1022 mr=map_rect_new(map, NULL);
1023 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1025 dbg(0,"while loop\n");
1026 while ((item=map_rect_get_item(mr))) {
1027 dbg(0,"item=%p\n", item);
1029 if (item->type != type_nav_position) {
1030 item_attr_get(item, attr_navigation_long, &attr);
1031 if (attr.u.str == NULL) {
1034 dbg(2, "Command='%s'\n", attr.u.str);
1035 param[0].value=g_strdup(attr.u.str);
1037 param[0].value=_("Position");
1038 param[0].name=_("Command");
1040 item_attr_get(item, attr_length, &attr);
1041 dbg(2, "Length=%d\n", attr.u.num);
1042 param[1].name=_("Length");
1044 if ( attr.u.num >= 2000 )
1046 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1050 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1053 item_attr_get(item, attr_time, &attr);
1054 dbg(2, "Time=%d\n", attr.u.num);
1056 param[2].name=_("Time");
1059 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1063 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1066 item_attr_get(item, attr_destination_length, &attr);
1067 dbg(2, "Destlength=%d\n", attr.u.num);
1068 param[3].name=_("Destination Length");
1069 if ( attr.u.num >= 2000 )
1071 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1075 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1078 item_attr_get(item, attr_destination_time, &attr);
1079 dbg(2, "Desttime=%d\n", attr.u.num);
1081 param[4].name=_("Destination Time");
1084 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1088 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1090 datawindow_add(this_->roadbook_window, param, 5);
1092 map_rect_destroy(mr);
1094 datawindow_mode(this_->roadbook_window, 0);
1098 navit_window_roadbook_destroy(struct navit *this_)
1101 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1102 this_->roadbook_window=NULL;
1103 this_->roadbook_callback=NULL;
1106 navit_window_roadbook_new(struct navit *this_)
1108 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1112 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1113 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1114 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1115 navit_window_roadbook_update(this_);
1119 navit_init(struct navit *this_)
1125 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1128 navit_destroy(this_);
1132 dbg(0,"no graphics\n");
1133 navit_destroy(this_);
1136 dbg(2,"Connecting gui to graphics\n");
1137 if (gui_set_graphics(this_->gui, this_->gra)) {
1138 struct attr attr_type_gui, attr_type_graphics;
1139 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1140 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1141 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1142 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1143 dbg(0," for explanations and solutions\n");
1145 navit_destroy(this_);
1148 dbg(2,"Initializing graphics\n");
1149 dbg(2,"Setting Vehicle\n");
1150 navit_set_vehicle(this_, this_->vehicle);
1151 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1152 if (this_->mapsets) {
1153 ms=this_->mapsets->data;
1155 if ((map=route_get_map(this_->route)))
1156 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1157 if ((map=route_get_graph_map(this_->route))) {
1158 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1159 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1161 route_set_mapset(this_->route, ms);
1162 route_set_projection(this_->route, transform_get_projection(this_->trans));
1164 if (this_->tracking) {
1165 tracking_set_mapset(this_->tracking, ms);
1167 tracking_set_route(this_->tracking, this_->route);
1169 if (this_->navigation) {
1170 if ((map=navigation_get_map(this_->navigation))) {
1171 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1172 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1175 if (this_->tracking) {
1176 if ((map=tracking_get_map(this_->tracking))) {
1177 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1178 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1181 navit_add_former_destinations_from_file(this_);
1184 struct attr callback;
1185 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1186 callback.type=attr_callback;
1187 callback.u.callback=this_->route_cb;
1188 route_add_attr(this_->route, &callback);
1190 if (this_->navigation) {
1191 if (this_->speech) {
1192 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1193 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1196 navigation_set_route(this_->navigation, this_->route);
1198 dbg(2,"Setting Center\n");
1199 char *center_file = bookmarks_get_center_file(FALSE);
1200 bookmarks_set_center_from_file(this_->bookmarks, center_file);
1201 g_free(center_file);
1203 if (this_->menubar) {
1204 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1206 navit_add_menu_windows_items(this_, men);
1212 navit_window_roadbook_new(this_);
1213 navit_window_items_new(this_);
1216 messagelist_init(this_->messages);
1218 navit_set_cursors(this_);
1220 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1221 callback=(this_->ready == 2);
1223 dbg(2,"ready=%d\n",this_->ready);
1224 if (this_->ready == 3)
1227 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1229 routech_test(this_);
1234 navit_zoom_to_route(struct navit *this_, int orientation)
1237 struct map_rect *mr=NULL;
1240 struct coord_rect r;
1241 int count=0,scale=16;
1245 map=route_get_map(this_->route);
1246 dbg(1,"map=%p\n",map);
1248 mr=map_rect_new(map, NULL);
1249 dbg(1,"mr=%p\n",mr);
1251 while ((item=map_rect_get_item(mr))) {
1252 dbg(1,"item=%s\n", item_to_name(item->type));
1253 while (item_coord_get(item, &c, 1)) {
1258 coord_rect_extend(&r, &c);
1265 c.x=(r.rl.x+r.lu.x)/2;
1266 c.y=(r.rl.y+r.lu.y)/2;
1267 dbg(1,"count=%d\n",count);
1268 if (orientation != -1)
1269 transform_set_yaw(this_->trans, orientation);
1270 transform_set_center(this_->trans, &c);
1271 dbg(1,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1272 while (scale < 1<<20) {
1274 transform_set_scale(this_->trans, scale);
1275 transform_setup_source_rect(this_->trans);
1276 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1277 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1278 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1279 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1280 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1286 if (this_->ready == 3)
1287 navit_draw_async(this_,0);
1291 navit_cmd_zoom_to_route(struct navit *this)
1293 navit_zoom_to_route(this, 0);
1298 * Change the current zoom level
1300 * @param navit The navit instance
1301 * @param center The point where to center the map, including its projection
1305 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1307 struct coord *c=transform_center(this_->trans);
1309 enum projection pro = transform_get_projection(this_->trans);
1310 if (pro != center->pro) {
1313 transform_from_to(&c1, center->pro, &c2, pro);
1320 navit_set_timeout(this_);
1321 if (this_->ready == 3)
1326 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1330 transform_set_center(this_->trans, c);
1331 transform_get_size(this_->trans, &width, &height);
1334 update_transformation(this_->trans, &po, p, NULL);
1336 navit_set_timeout(this_);
1340 * Links all vehicles to a cursor depending on the current profile.
1342 * @param this_ A navit instance
1343 * @author Ralph Sennhauser (10/2009)
1346 navit_set_cursors(struct navit *this_)
1349 struct navit_vehicle *nv;
1353 v=g_list_first(this_->vehicles); // GList of navit_vehicles
1356 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL))
1357 c=layout_get_cursor(this_->layout_current, name.u.str);
1359 c=layout_get_cursor(this_->layout_current, "default");
1360 vehicle_set_cursor(nv->vehicle, c);
1367 navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir)
1370 struct navit_vehicle *nv=this_->vehicle;
1372 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
1373 #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 */
1374 float min_offset = 0.; // Percent offset at min_offset_speed.
1375 float max_offset = 30.; // Percent offset at max_offset_speed.
1376 int min_offset_speed = 2; // Speed in km/h
1377 int max_offset_speed = 50; // Speed ini km/h
1378 // Calculate cursor offset from the center of the screen, upon speed.
1379 if (nv->speed <= min_offset_speed) {
1380 offset = min_offset;
1381 } else if (nv->speed > max_offset_speed) {
1382 offset = max_offset;
1384 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1388 transform_get_size(this_->trans, &width, &height);
1389 if (this_->orientation == -1) {
1391 p->y=(50 + offset)*height/100;
1396 if (this_->tracking && this_->tracking_flag) {
1397 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1399 mdir=nv->dir-this_->orientation;
1402 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1403 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1405 *dir=this_->orientation;
1411 navit_set_center_cursor(struct navit *this_)
1415 struct navit_vehicle *nv=this_->vehicle;
1416 navit_get_cursor_pnt(this_, &pn, &dir);
1417 transform_set_yaw(this_->trans, dir);
1418 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1419 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1420 if (this_->ready == 3)
1421 navit_draw_async(this_, 1);
1425 navit_cmd_set_center_cursor(struct navit *this_)
1427 navit_set_center_cursor(this_);
1431 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1435 transform_reverse(this_->trans, p, &c);
1438 pc.pro = transform_get_projection(this_->trans);
1439 navit_set_center(this_, &pc, set_timeout);
1443 switch((*attrs)->type) {
1445 zoom=(*attrs)->u.num;
1448 g=*((*attrs)->u.coord_geo);
1453 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1455 int dir=0, orient_old=0, attr_updated=0;
1459 struct navit_vehicle *nv;
1460 struct attr active=(struct attr){attr_active,{(void *)0}};
1463 switch (attr->type) {
1465 attr_updated=(this_->autozoom_secs != attr->u.num);
1466 this_->autozoom_secs = attr->u.num;
1468 case attr_autozoom_active:
1469 attr_updated=(this_->autozoom_active != attr->u.num);
1470 this_->autozoom_active = attr->u.num;
1473 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1474 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1475 transform_set_center(this_->trans, &co);
1477 case attr_drag_bitmap:
1478 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1479 this_->drag_bitmap=!!attr->u.num;
1481 case attr_flags_graphics:
1482 attr_updated=(this_->graphics_flags != attr->u.num);
1483 this_->graphics_flags=attr->u.num;
1486 if (!this_->vehicle)
1488 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1489 this_->vehicle->follow_curr = attr->u.num;
1492 if(this_->layout_current!=attr->u.layout) {
1493 this_->layout_current=attr->u.layout;
1494 graphics_font_destroy_all(this_->gra);
1495 navit_set_cursors(this_);
1496 if (this_->ready == 3)
1501 case attr_layout_name:
1505 if (!strcmp(lay->name,attr->u.str)) {
1507 attr.type=attr_layout;
1509 return navit_set_attr_do(this_, &attr, init);
1514 case attr_orientation:
1515 orient_old=this_->orientation;
1516 this_->orientation=attr->u.num;
1518 if (this_->orientation != -1) {
1519 dir = this_->orientation;
1521 if (this_->vehicle) {
1522 dir = this_->vehicle->dir;
1525 transform_set_yaw(this_->trans, dir);
1526 if (orient_old != this_->orientation) {
1528 if (this_->ready == 3)
1535 case attr_osd_configuration:
1536 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1537 attr_updated=(this_->osd_configuration != attr->u.num);
1538 this_->osd_configuration=attr->u.num;
1541 attr_updated=(this_->pitch != attr->u.num);
1542 this_->pitch=attr->u.num;
1543 transform_set_pitch(this_->trans, this_->pitch);
1544 if (!init && attr_updated && this_->ready == 3)
1547 case attr_projection:
1548 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1549 navit_projection_set(this_, attr->u.projection, !init);
1554 attr_updated=(this_->radius != attr->u.num);
1555 this_->radius=attr->u.num;
1557 case attr_recent_dest:
1558 attr_updated=(this_->recentdest_count != attr->u.num);
1559 this_->recentdest_count=attr->u.num;
1562 if(this_->speech && this_->speech != attr->u.speech) {
1564 this_->speech = attr->u.speech;
1568 attr_updated=(this_->center_timeout != attr->u.num);
1569 this_->center_timeout = attr->u.num;
1572 attr_updated=(this_->tracking_flag != !!attr->u.num);
1573 this_->tracking_flag=!!attr->u.num;
1575 case attr_use_mousewheel:
1576 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1577 this_->use_mousewheel=!!attr->u.num;
1583 if (nv->vehicle == attr->u.vehicle) {
1584 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1586 vehicle_set_attr(this_->vehicle->vehicle, &active);
1588 vehicle_set_attr(nv->vehicle, &active);
1591 navit_set_vehicle(this_, nv);
1597 zoom=transform_get_scale(this_->trans);
1598 attr_updated=(zoom != attr->u.num);
1599 transform_set_scale(this_->trans, attr->u.num);
1600 if (attr_updated && !init)
1604 attr_updated=(attr->u.num != this_->zoom_min);
1605 this_->zoom_min=attr->u.num;
1608 attr_updated=(attr->u.num != this_->zoom_max);
1609 this_->zoom_max=attr->u.num;
1612 navit_add_message(this_, attr->u.str);
1614 case attr_follow_cursor:
1615 attr_updated=(this_->follow_cursor != !!attr->u.num);
1616 this_->follow_cursor=!!attr->u.num;
1621 if (attr_updated && !init) {
1622 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1623 if (attr->type == attr_osd_configuration)
1624 graphics_draw_mode(this_->gra, draw_mode_end);
1630 navit_set_attr(struct navit *this_, struct attr *attr)
1632 return navit_set_attr_do(this_, attr, 0);
1636 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1638 struct message *msg;
1644 msg = navit_get_messages(this_);
1652 len += strlen(msg->text) + 1;
1655 attr->u.str = g_malloc(len + 1);
1657 msg = navit_get_messages(this_);
1660 g_stpcpy((attr->u.str + offset), msg->text);
1661 offset += strlen(msg->text);
1662 attr->u.str[offset] = '\n';
1668 attr->u.str[len] = '\0';
1670 case attr_bookmark_map:
1671 attr->u.map=bookmarks_get_map(this_->bookmarks);
1673 case attr_bookmarks:
1674 attr->u.bookmarks=this_->bookmarks;
1676 case attr_callback_list:
1677 attr->u.callback_list=this_->attr_cbl;
1679 case attr_destination:
1680 if (! this_->destination_valid)
1682 attr->u.pcoord=&this_->destination;
1684 case attr_displaylist:
1685 attr->u.displaylist=this_->displaylist;
1686 return (attr->u.displaylist != NULL);
1688 if (!this_->vehicle)
1690 attr->u.num=this_->vehicle->follow_curr;
1692 case attr_former_destination_map:
1693 attr->u.map=this_->former_destination;
1696 attr->u.graphics=this_->gra;
1697 ret=(attr->u.graphics != NULL);
1700 attr->u.gui=this_->gui;
1701 ret=(attr->u.gui != NULL);
1706 iter->u.list=g_list_next(iter->u.list);
1708 iter->u.list=this_->layouts;
1712 attr->u.layout=(struct layout *)iter->u.list->data;
1714 attr->u.layout=this_->layout_current;
1718 if (iter && this_->mapsets) {
1719 if (!iter->u.mapset_handle) {
1720 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1722 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1724 mapset_close(iter->u.mapset_handle);
1732 attr->u.mapset=this_->mapsets->data;
1733 ret=(attr->u.mapset != NULL);
1735 case attr_navigation:
1736 attr->u.navigation=this_->navigation;
1738 case attr_orientation:
1739 attr->u.num=this_->orientation;
1741 case attr_osd_configuration:
1742 attr->u.num=this_->osd_configuration;
1745 attr->u.num=transform_get_pitch(this_->trans);
1747 case attr_projection:
1749 attr->u.num=transform_get_projection(this_->trans);
1755 attr->u.route=this_->route;
1758 attr->u.speech=this_->speech;
1761 attr->u.num=this_->tracking_flag;
1763 case attr_transformation:
1764 attr->u.transformation=this_->trans;
1769 iter->u.list=g_list_next(iter->u.list);
1771 iter->u.list=this_->vehicles;
1775 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1777 if(this_->vehicle) {
1778 attr->u.vehicle=this_->vehicle->vehicle;
1785 attr->u.num=transform_get_scale(this_->trans);
1787 case attr_autozoom_active:
1788 attr->u.num=this_->autozoom_active;
1790 case attr_follow_cursor:
1791 attr->u.num=this_->follow_cursor;
1801 navit_add_log(struct navit *this_, struct log *log)
1803 struct attr type_attr;
1804 if (!log_get_attr(log, attr_type, &type_attr, NULL))
1806 if (!strcmp(type_attr.u.str, "textfile_debug")) {
1807 char *header = "type=track_tracked\n";
1808 if (this_->textfile_debug_log)
1810 log_set_header(log, header, strlen(header));
1811 this_->textfile_debug_log=log;
1818 navit_add_attr(struct navit *this_, struct attr *attr)
1821 switch (attr->type) {
1823 navit_add_callback(this_, attr->u.callback);
1826 ret=navit_add_log(this_, attr->u.log);
1829 ret=navit_set_gui(this_, attr->u.gui);
1832 ret=navit_set_graphics(this_, attr->u.graphics);
1835 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1836 if(!this_->layout_current)
1837 this_->layout_current=attr->u.layout;
1840 this_->route=attr->u.route;
1843 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1845 case attr_navigation:
1846 this_->navigation=attr->u.navigation;
1848 case attr_recent_dest:
1849 this_->recentdest_count = attr->u.num;
1852 this_->speech=attr->u.speech;
1855 this_->tracking=attr->u.tracking;
1858 ret=navit_add_vehicle(this_, attr->u.vehicle);
1860 case attr_vehicleprofile:
1861 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
1863 case attr_autozoom_min:
1864 this_->autozoom_min = attr->u.num;
1869 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1874 navit_remove_attr(struct navit *this_, struct attr *attr)
1877 switch (attr->type) {
1879 navit_remove_callback(this_, attr->u.callback);
1888 navit_attr_iter_new(void)
1890 return g_new0(struct attr_iter, 1);
1894 navit_attr_iter_destroy(struct attr_iter *iter)
1900 navit_add_callback(struct navit *this_, struct callback *cb)
1902 callback_list_add(this_->attr_cbl, cb);
1906 navit_remove_callback(struct navit *this_, struct callback *cb)
1908 callback_list_remove(this_->attr_cbl, cb);
1912 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
1914 * @param navit The navit instance
1919 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
1921 struct point cursor_pnt;
1922 enum projection pro;
1929 pro=transform_get_projection(this_->trans);
1930 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1932 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
1937 pro=transform_get_projection(this_->trans);
1938 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
1941 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
1943 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
1949 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
1951 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
1952 struct pcoord cursor_pc;
1953 struct point cursor_pnt, *pnt=&cursor_pnt;
1954 struct tracking *tracking=NULL;
1955 enum projection pro=transform_get_projection(this_->trans);
1957 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
1961 if (this_->ready != 3) {
1962 profile(0,"return 1\n");
1965 navit_layout_switch(this_);
1966 if (this_->vehicle == nv && this_->tracking_flag)
1967 tracking=this_->tracking;
1969 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
1970 attr_object=tracking;
1971 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
1973 attr_object=nv->vehicle;
1974 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
1976 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
1977 if (!attr_valid.u.num != attr_position_valid_invalid)
1979 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
1980 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
1981 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
1982 profile(0,"return 2\n");
1985 nv->dir=*attr_dir.u.numd;
1986 nv->speed=*attr_speed.u.numd;
1987 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
1988 if (nv != this_->vehicle) {
1989 navit_vehicle_draw(this_, nv, NULL);
1990 profile(0,"return 3\n");
1993 cursor_pc.x = nv->coord.x;
1994 cursor_pc.y = nv->coord.y;
1995 cursor_pc.pro = pro;
1998 route_set_position_from_tracking(this_->route, tracking, pro);
2000 route_set_position(this_->route, &cursor_pc);
2002 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2003 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2004 if (this_->gui && nv->speed > 2)
2005 gui_disable_suspend(this_->gui);
2007 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2008 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2009 (nv->follow_curr == 1 || !transform_within_border(this_->trans, &cursor_pnt, border)))
2010 navit_set_center_cursor(this_);
2012 navit_vehicle_draw(this_, nv, pnt);
2014 if (nv->follow_curr > 1)
2017 nv->follow_curr=nv->follow;
2018 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2020 /* Finally, if we reached our destination, stop navigation. */
2021 if (this_->route && route_destination_reached(this_->route)) {
2022 navit_set_destination(this_, NULL, NULL, 0);
2024 profile(0,"return 5\n");
2028 * Set the position of the vehicle
2030 * @param navit The navit instance
2031 * @param c The coordinate to set as position
2036 navit_set_position(struct navit *this_, struct pcoord *c)
2039 route_set_position(this_->route, c);
2040 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2042 if (this_->ready == 3)
2047 navit_set_vehicleprofile(struct navit *this_, char *name)
2051 l=this_->vehicleprofiles;
2053 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2054 if (!strcmp(attr.u.str, name)) {
2055 this_->vehicleprofile=l->data;
2057 route_set_profile(this_->route, this_->vehicleprofile);
2067 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2071 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2072 if (navit_set_vehicleprofile(this_, attr.u.str))
2075 navit_set_vehicleprofile(this_,"car");
2079 * Register a new vehicle
2081 * @param navit The navit instance
2082 * @param v The vehicle instance
2083 * @returns 1 for success
2086 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2088 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2089 struct attr follow, active, animate;
2094 nv->animate_cursor=0;
2095 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2096 nv->follow=nv->follow=follow.u.num;
2097 nv->follow_curr=nv->follow;
2098 this_->vehicles=g_list_append(this_->vehicles, nv);
2099 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2100 navit_set_vehicle(this_, nv);
2101 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2102 nv->animate_cursor=animate.u.num;
2103 nv->callback.type=attr_callback;
2104 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2105 vehicle_add_attr(nv->vehicle, &nv->callback);
2106 vehicle_set_attr(nv->vehicle, &this_->self);
2114 navit_get_gui(struct navit *this_)
2119 struct transformation *
2120 navit_get_trans(struct navit *this_)
2122 return this_->trans;
2126 navit_get_route(struct navit *this_)
2128 return this_->route;
2132 navit_get_navigation(struct navit *this_)
2134 return this_->navigation;
2137 struct displaylist *
2138 navit_get_displaylist(struct navit *this_)
2140 return this_->displaylist;
2144 navit_layout_switch(struct navit *n)
2148 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2149 double trise,tset,trise_actual;
2151 int year, month, day;
2153 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2154 return; //No layout - nothing to switch
2158 l=layout_attr.u.layout;
2160 if (l->dayname || l->nightname) {
2161 //Ok, we know that we have profile to switch
2163 //Check that we aren't calculating too fast
2164 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2165 currTs=iso8601_to_secs(iso8601_attr.u.str);
2166 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2168 if (currTs-(n->prevTs)<60) {
2169 //We've have to wait a little
2172 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2174 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2175 //No position - no sun
2178 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2179 return; //No valid fix yet
2182 //We calculate sunrise anyway, cause it is needed both for day and for night
2183 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,35,1,&trise,&tset)!=0) {
2184 //near the pole sun never rises/sets, so we should never switch profiles
2190 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2193 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2194 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2195 //The sun is rising now!
2196 if (strcmp(l->name,l->dayname)) {
2197 navit_set_layout_by_name(n,l->dayname);
2202 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,-24,0,&trise,&tset)!=0) {
2203 //near the pole sun never rises/sets, so we should never switch profiles
2207 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2208 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2209 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
2210 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2212 if (strcmp(l->name,l->nightname)) {
2213 navit_set_layout_by_name(n,l->nightname);
2223 navit_set_vehicle_by_name(struct navit *n,const char *name)
2226 struct attr_iter *iter;
2227 struct attr vehicle_attr, name_attr;
2229 iter=navit_attr_iter_new();
2231 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2232 v=vehicle_attr.u.vehicle;
2233 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2234 if (name_attr.type==attr_name) {
2235 if (!strcmp(name,name_attr.u.str)) {
2236 navit_set_attr(n,&vehicle_attr);
2237 navit_attr_iter_destroy(iter);
2242 navit_attr_iter_destroy(iter);
2247 navit_set_layout_by_name(struct navit *n,const char *name)
2250 struct attr_iter iter;
2251 struct attr layout_attr;
2255 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2256 return 0; //No layouts - nothing to do
2258 if (iter.u.list==NULL) {
2262 iter.u.list=g_list_first(iter.u.list);
2264 while(iter.u.list) {
2265 l=(struct layout*)iter.u.list->data;
2266 if (!strcmp(name,l->name)) {
2267 layout_attr.u.layout=l;
2268 layout_attr.type=attr_layout;
2269 navit_set_attr(n,&layout_attr);
2270 iter.u.list=g_list_first(iter.u.list);
2273 iter.u.list=g_list_next(iter.u.list);
2276 iter.u.list=g_list_first(iter.u.list);
2281 navit_block(struct navit *this_, int block)
2284 this_->blocked |= 1;
2285 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2286 this_->blocked |= 2;
2289 if (this_->blocked & 2) {
2299 navit_destroy(struct navit *this_)
2301 /* TODO: destroy objects contained in this_ */
2303 vehicle_destroy(this_->vehicle->vehicle);
2304 if (this_->bookmarks) {
2305 char *center_file = bookmarks_get_center_file(TRUE);
2306 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2307 g_free(center_file);
2308 bookmarks_destroy(this_->bookmarks);
2310 callback_destroy(this_->nav_speech_cb);
2311 callback_destroy(this_->roadbook_callback);
2312 callback_destroy(this_->popup_callback);
2313 callback_destroy(this_->motion_timeout_callback);
2315 graphics_remove_callback(this_->gra, this_->resize_callback);
2316 callback_destroy(this_->resize_callback);
2318 graphics_remove_callback(this_->gra, this_->button_callback);
2319 callback_destroy(this_->button_callback);
2321 graphics_remove_callback(this_->gra, this_->motion_callback);
2322 callback_destroy(this_->motion_callback);