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;
142 struct gui *main_loop_gui;
147 struct mapset_handle *mapset_handle;
151 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
152 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
153 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
154 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
155 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir);
156 static void navit_set_cursors(struct navit *this_);
157 static void navit_cmd_zoom_to_route(struct navit *this);
158 static void navit_cmd_set_center_cursor(struct navit *this_);
159 static void navit_cmd_announcer_toggle(struct navit *this_);
160 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
163 navit_add_mapset(struct navit *this_, struct mapset *ms)
165 this_->mapsets = g_list_append(this_->mapsets, ms);
169 navit_get_mapset(struct navit *this_)
172 return this_->mapsets->data;
174 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
180 navit_get_tracking(struct navit *this_)
182 return this_->tracking;
186 navit_draw_async(struct navit *this_, int async)
189 struct navit_vehicle *nv;
191 if (this_->blocked) {
195 transform_setup_source_rect(this_->trans);
199 navit_vehicle_draw(this_, nv, NULL);
202 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
206 navit_draw(struct navit *this_)
208 navit_draw_async(this_, 0);
212 navit_get_ready(struct navit *this_)
220 navit_draw_displaylist(struct navit *this_)
222 if (this_->ready == 3)
223 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
227 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
230 if (attr->type != attr_route_status)
233 if (this_->ready != 3)
235 if (updated != route_status_path_done_new)
237 if (this_->vehicle) {
238 if (this_->vehicle->follow_curr == 1)
240 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
241 this_->vehicle->follow_curr=this_->vehicle->follow;
247 navit_handle_resize(struct navit *this_, int w, int h)
249 struct map_selection sel;
251 if (this_->w != w || this_->h != h) {
252 memset(&sel, 0, sizeof(sel));
257 transform_set_screen_selection(this_->trans, &sel);
258 graphics_init(this_->gra);
259 graphics_set_rect(this_->gra, &sel.u.p_rect);
261 if (this_->ready == 3)
266 navit_resize(void *data, int w, int h)
268 struct navit *this=data;
269 if (!this->ignore_graphics_events)
270 navit_handle_resize(this, w, h);
274 navit_get_width(struct navit *this_)
281 navit_get_height(struct navit *this_)
287 navit_popup(void *data)
289 struct navit *this_=data;
290 popup(this_, 1, &this_->pressed);
291 this_->button_timeout=NULL;
297 navit_ignore_button(struct navit *this_)
299 if (this_->ignore_button)
301 this_->ignore_button=1;
306 navit_ignore_graphics_events(struct navit *this_, int ignore)
308 this_->ignore_graphics_events=ignore;
312 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
317 double angleo,anglen;
319 if (!transform_reverse(tr, old, &co))
322 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
323 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
324 yaw=transform_get_yaw(tr)+angleo-anglen;
325 transform_set_yaw(tr, yaw % 360);
327 if (!transform_reverse(tr, new, &cn))
329 cp=transform_get_center(tr);
332 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
333 transform_set_center(tr, &c);
337 navit_set_timeout(struct navit *this_)
340 follow.type=attr_follow;
341 follow.u.num=this_->center_timeout;
342 navit_set_attr(this_, &follow);
346 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
350 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
351 if (this_->ignore_button) {
352 this_->ignore_button=0;
360 this_->button_pressed=1;
364 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
367 navit_set_center_screen(this_, p, 1);
369 popup(this_, button, p);
370 if (button == 4 && this_->use_mousewheel) {
372 navit_zoom_in(this_, 2, p);
374 if (button == 5 && this_->use_mousewheel) {
376 navit_zoom_out(this_, 2, p);
380 this_->button_pressed=0;
381 if (this_->button_timeout) {
382 event_remove_timeout(this_->button_timeout);
383 this_->button_timeout=NULL;
384 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
385 navit_set_center_screen(this_, p, !this_->zoomed);
388 if (this_->motion_timeout) {
389 event_remove_timeout(this_->motion_timeout);
390 this_->motion_timeout=NULL;
397 update_transformation(this_->trans, &this_->pressed, p, &pr);
399 update_transformation(this_->trans, &this_->pressed, p, NULL);
401 graphics_draw_drag(this_->gra, NULL);
402 graphics_overlay_disable(this_->gra, 0);
404 navit_set_timeout(this_);
413 navit_button(void *data, int pressed, int button, struct point *p)
415 struct navit *this=data;
416 if (!this->ignore_graphics_events) {
417 if (! this->popup_callback)
418 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
419 navit_handle_button(this, pressed, button, p, this->popup_callback);
425 navit_motion_timeout(struct navit *this_)
429 if (this_->drag_bitmap) {
431 point.x=(this_->current.x-this_->pressed.x);
432 point.y=(this_->current.y-this_->pressed.y);
433 if (graphics_draw_drag(this_->gra, &point)) {
434 graphics_overlay_disable(this_->gra, 1);
435 graphics_draw_mode(this_->gra, draw_mode_end);
437 this_->motion_timeout=NULL;
441 dx=(this_->current.x-this_->last.x);
442 dy=(this_->current.y-this_->last.y);
444 struct transformation *tr;
446 this_->last=this_->current;
447 graphics_overlay_disable(this_->gra, 1);
448 tr=transform_dup(this_->trans);
452 update_transformation(tr, &this_->pressed, &this_->current, &pr);
454 update_transformation(tr, &this_->pressed, &this_->current, NULL);
457 graphics_displaylist_move(this_->displaylist, dx, dy);
459 graphics_draw_cancel(this_->gra, this_->displaylist);
460 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
461 transform_destroy(tr);
464 this_->motion_timeout=NULL;
469 navit_handle_motion(struct navit *this_, struct point *p)
473 if (this_->button_pressed && !this_->popped) {
474 dx=(p->x-this_->pressed.x);
475 dy=(p->y-this_->pressed.y);
476 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
477 if (this_->button_timeout) {
478 event_remove_timeout(this_->button_timeout);
479 this_->button_timeout=NULL;
482 if (! this_->motion_timeout_callback)
483 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
484 if (! this_->motion_timeout)
485 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
491 navit_motion(void *data, struct point *p)
493 struct navit *this=data;
494 if (!this->ignore_graphics_events)
495 navit_handle_motion(this, p);
499 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
501 struct coord c1, c2, *center;
503 transform_reverse(this_->trans, p, &c1);
504 transform_set_scale(this_->trans, scale);
506 transform_reverse(this_->trans, p, &c2);
507 center = transform_center(this_->trans);
508 center->x += c1.x - c2.x;
509 center->y += c1.y - c2.y;
516 * @brief Automatically adjusts zoom level
518 * This function automatically adjusts the current
519 * zoom level according to the current speed.
521 * @param this_ The navit struct
522 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
523 * @param speed The vehicles speed in meters per second
524 * @param dir The direction into which the vehicle moves
527 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
534 if (! this_->autozoom_active) {
538 distance = speed * this_->autozoom_secs;
540 transform_get_size(this_->trans, &w, &h);
541 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
542 scale = transform_get_scale(this_->trans);
544 /* We make sure that the point we want to see is within a certain range
545 * around the vehicle. The radius of this circle is the size of the
546 * screen. This doesn't necessarily mean the point is visible because of
547 * perspective etc. Quite rough, but should be enough. */
550 new_scale = (double)distance / h * 16;
552 new_scale = (double)distance / w * 16;
555 if (abs(new_scale - scale) < 2) {
559 if (new_scale >= this_->autozoom_min) {
560 navit_scale(this_, (long)new_scale, &pc, 0);
562 if (scale != this_->autozoom_min) {
563 navit_scale(this_, this_->autozoom_min, &pc, 0);
569 * Change the current zoom level, zooming closer to the ground
571 * @param navit The navit instance
572 * @param factor The zoom factor, usually 2
573 * @param p The invariant point (if set to NULL, default to center)
577 navit_zoom_in(struct navit *this_, int factor, struct point *p)
579 long scale=transform_get_scale(this_->trans)/factor;
582 navit_scale(this_, scale, p, 1);
586 * Change the current zoom level
588 * @param navit The navit instance
589 * @param factor The zoom factor, usually 2
590 * @param p The invariant point (if set to NULL, default to center)
594 navit_zoom_out(struct navit *this_, int factor, struct point *p)
596 long scale=transform_get_scale(this_->trans)*factor;
597 navit_scale(this_, scale, p, 1);
601 navit_cmd_zoom_in(struct navit *this_)
604 if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
605 navit_zoom_in(this_, 2, &p);
606 this_->vehicle->follow_curr=this_->vehicle->follow;
608 navit_zoom_in(this_, 2, NULL);
613 navit_cmd_zoom_out(struct navit *this_)
616 if (this_->vehicle && this_->vehicle->follow_curr == 1 && navit_get_cursor_pnt(this_, &p, NULL)) {
617 navit_zoom_out(this_, 2, &p);
618 this_->vehicle->follow_curr=this_->vehicle->follow;
620 navit_zoom_out(this_, 2, NULL);
624 static struct command_table commands[] = {
625 {"zoom_in",command_cast(navit_cmd_zoom_in)},
626 {"zoom_out",command_cast(navit_cmd_zoom_out)},
627 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
628 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
629 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
634 navit_new(struct attr *parent, struct attr **attrs)
636 struct navit *this_=g_new0(struct navit, 1);
637 struct pcoord center;
640 enum projection pro=projection_mg;
645 this_->self.type=attr_navit;
646 this_->self.u.navit=this_;
647 this_->attr_cbl=callback_list_new();
649 this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
651 this_->orientation=-1;
652 this_->tracking_flag=1;
653 this_->recentdest_count=10;
654 this_->osd_configuration=-1;
656 this_->center_timeout = 10;
657 this_->use_mousewheel = 1;
658 this_->autozoom_secs = 10;
659 this_->autozoom_min = 7;
660 this_->autozoom_active = 0;
661 this_->follow_cursor = 1;
663 this_->trans = transform_new();
664 transform_from_geo(pro, &g, &co);
671 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
672 for (;*attrs; attrs++) {
673 navit_set_attr_do(this_, *attrs, 1);
675 this_->displaylist=graphics_displaylist_new();
676 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
678 this_->messages = messagelist_new(attrs);
684 navit_set_gui(struct navit *this_, struct gui *gui)
689 if (gui_has_main_loop(this_->gui)) {
690 if (! main_loop_gui) {
691 main_loop_gui=this_->gui;
693 dbg(0,"gui with main loop already active, ignoring this instance");
701 navit_add_message(struct navit *this_, char *message)
703 message_new(this_->messages, message);
707 *navit_get_messages(struct navit *this_)
709 return message_get(this_->messages);
713 navit_set_graphics(struct navit *this_, struct graphics *gra)
718 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
719 graphics_add_callback(gra, this_->resize_callback);
720 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
721 graphics_add_callback(gra, this_->button_callback);
722 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
723 graphics_add_callback(gra, this_->motion_callback);
728 navit_get_graphics(struct navit *this_)
733 struct vehicleprofile *
734 navit_get_vehicleprofile(struct navit *this_)
736 return this_->vehicleprofile;
740 navit_get_vehicleprofiles(struct navit *this_)
742 return this_->vehicleprofiles;
746 navit_projection_set(struct navit *this_, enum projection pro)
751 c=transform_center(this_->trans);
752 transform_to_geo(transform_get_projection(this_->trans), c, &g);
753 transform_set_projection(this_->trans, pro);
754 transform_from_geo(pro, &g, c);
759 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
762 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
777 while ((ch = fgetc(f)) != EOF) {
778 if ((ch == '\n') && (prev != '\n')) {
784 if (prev != '\n') { // Last line did not end with a newline
789 f = fopen(file, "r+");
791 while (lines >= limit) { // We have to "scroll up"
793 numc = 0; // Counts how many bytes we have in our line to scroll up
794 while ((ch = fgetc(f)) != EOF) {
801 buffer=g_malloc(numc);
802 offset = numc; // Offset holds where we currently are
805 fseek(f,offset,SEEK_SET);
806 readc = fread(buffer,1,numc,f);
808 fseek(f,-(numc+readc),SEEK_CUR);
809 fwrite(buffer,1,readc,f);
812 } while (readc == numc);
816 ftruncate(fd,(offset-numc));
830 prostr = projection_to_name(c->pro,NULL);
831 fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
832 prostr, *prostr ? ":" : "",
833 c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
834 c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y,
843 * navit_get_user_data_directory
845 * returns the directory used to store user data files (center.txt,
846 * destination.txt, bookmark.txt, ...)
848 * arg: gboolean create: create the directory if it does not exist
851 navit_get_user_data_directory(gboolean create) {
853 dir = getenv("NAVIT_USER_DATADIR");
854 if (create && !file_exists(dir)) {
855 dbg(0,"creating dir %s\n", dir);
856 if (file_mkdir(dir,0)) {
857 dbg(0,"failed creating dir %s\n", dir);
866 * navit_get_destination_file
868 * returns the name of the file used to store destinations with its
871 * arg: gboolean create: create the directory where the file is stored
872 * if it does not exist
875 navit_get_destination_file(gboolean create)
877 return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
881 * navit_get_bookmark_file
883 * returns the name of the file used to store bookmarks with its
886 * arg: gboolean create: create the directory where the file is stored
887 * if it does not exist
890 navit_get_bookmark_file(gboolean create)
892 return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
897 * navit_get_bookmark_file
899 * returns the name of the file used to store the center file with its
902 * arg: gboolean create: create the directory where the file is stored
903 * if it does not exist
906 navit_get_center_file(gboolean create)
908 return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
912 navit_set_center_from_file(struct navit *this_, char *file)
914 #ifndef HAVE_API_ANDROID
918 size_t line_size = 0;
920 struct coord *center;
922 f = fopen(file, "r");
925 getline(&line, &line_size, f);
928 center = transform_center(this_->trans);
929 pro = transform_get_projection(this_->trans);
930 coord_parse(g_strchomp(line), pro, center);
938 navit_write_center_to_file(struct navit *this_, char *file)
942 struct coord *center;
944 f = fopen(file, "w+");
946 center = transform_center(this_->trans);
947 pro = transform_get_projection(this_->trans);
948 coord_print(pro, center, f);
958 * Start the route computing to a given set of coordinates
960 * @param navit The navit instance
961 * @param c The coordinate to start routing to
962 * @param description A label which allows the user to later identify this destination in the former destinations selection
966 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
969 this_->destination=*c;
970 this_->destination_valid=1;
972 this_->destination_valid=0;
973 char *destination_file = navit_get_destination_file(TRUE);
974 navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
975 g_free(destination_file);
976 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
978 route_set_destination(this_->route, c, async);
980 if (this_->ready == 3)
986 * @brief Checks if a route is calculated
988 * This function checks if a route is calculated.
990 * @param this_ The navit struct whose route should be checked.
991 * @return True if the route is set, false otherwise.
994 navit_check_route(struct navit *this_)
997 return route_get_path_set(this_->route);
1004 * Record the given set of coordinates as a bookmark
1006 * @param navit The navit instance
1007 * @param c The coordinate to store
1008 * @param description A label which allows the user to later identify this bookmark
1012 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
1014 char *bookmark_file = navit_get_bookmark_file(TRUE);
1015 navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
1016 g_free(bookmark_file);
1018 callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
1021 struct navit *global_navit;
1024 navit_add_bookmarks_from_file(struct navit *this_)
1026 char *bookmark_file = navit_get_bookmark_file(FALSE);
1027 struct attr parent={attr_navit, .u.navit=this_};
1028 struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
1029 struct attr *attrs[]={&type, &data, NULL};
1031 this_->bookmark=map_new(&parent, attrs);
1032 g_free(bookmark_file);
1036 navit_former_destinations_active(struct navit *this_)
1038 char *destination_file = navit_get_destination_file(FALSE);
1042 f=fopen(destination_file,"r");
1044 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
1048 g_free(destination_file);
1053 navit_add_former_destinations_from_file(struct navit *this_)
1055 char *destination_file = navit_get_destination_file(FALSE);
1056 struct attr parent={attr_navit, .u.navit=this_};
1057 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
1058 struct attr *attrs[]={&type, &data, NULL};
1059 struct map_rect *mr;
1065 this_->former_destination=map_new(&parent, attrs);
1066 g_free(destination_file);
1067 if (!this_->route || !navit_former_destinations_active(this_))
1069 mr=map_rect_new(this_->former_destination, NULL);
1070 while ((item=map_rect_get_item(mr))) {
1071 if (item->type == type_former_destination && item_coord_get(item, &c, 1))
1074 map_rect_destroy(mr);
1075 pc.pro=map_projection(this_->former_destination);
1079 route_set_destination(this_->route, &pc, 1);
1080 this_->destination=pc;
1081 this_->destination_valid=1;
1087 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1092 if (this_->textfile_debug_log && this_->vehicle) {
1093 str1=g_strdup_vprintf(fmt, ap);
1094 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1095 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1103 navit_speech_estimate(struct navit *this_, char *str)
1105 return speech_estimate_duration(this_->speech, str);
1109 navit_say(struct navit *this_, char *text)
1111 speech_say(this_->speech, text);
1115 * @brief Toggles the navigation announcer for navit
1116 * @param this_ The navit object
1119 navit_cmd_announcer_toggle(struct navit *this_)
1121 struct attr attr, speechattr;
1123 // search for the speech attribute
1124 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1126 // find out if the corresponding attribute attr_active has been set
1127 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1129 attr.u.num = !attr.u.num;
1131 // otherwise disable it because voice is enabled by default
1132 attr.type = attr_active;
1136 // apply the new state
1137 if(!speech_set_attr(speechattr.u.speech, &attr))
1140 // announce that the speech attribute has changed
1141 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1145 navit_speak(struct navit *this_)
1147 struct navigation *nav=this_->navigation;
1148 struct map *map=NULL;
1149 struct map_rect *mr=NULL;
1153 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1155 dbg(1, "this_.speech->active %i\n", attr.u.num);
1160 map=navigation_get_map(nav);
1162 mr=map_rect_new(map, NULL);
1164 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1165 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1166 speech_say(this_->speech, attr.u.str);
1167 navit_add_message(this_, attr.u.str);
1168 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1170 map_rect_destroy(mr);
1175 navit_window_roadbook_update(struct navit *this_)
1177 struct navigation *nav=this_->navigation;
1178 struct map *map=NULL;
1179 struct map_rect *mr=NULL;
1182 struct param_list param[5];
1186 datawindow_mode(this_->roadbook_window, 1);
1188 map=navigation_get_map(nav);
1190 mr=map_rect_new(map, NULL);
1191 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1193 dbg(0,"while loop\n");
1194 while ((item=map_rect_get_item(mr))) {
1195 dbg(0,"item=%p\n", item);
1197 if (item->type != type_nav_position) {
1198 item_attr_get(item, attr_navigation_long, &attr);
1199 if (attr.u.str == NULL) {
1202 dbg(2, "Command='%s'\n", attr.u.str);
1203 param[0].value=g_strdup(attr.u.str);
1205 param[0].value=_("Position");
1206 param[0].name=_("Command");
1208 item_attr_get(item, attr_length, &attr);
1209 dbg(2, "Length=%d\n", attr.u.num);
1210 param[1].name=_("Length");
1212 if ( attr.u.num >= 2000 )
1214 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1218 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1221 item_attr_get(item, attr_time, &attr);
1222 dbg(2, "Time=%d\n", attr.u.num);
1224 param[2].name=_("Time");
1227 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1231 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1234 item_attr_get(item, attr_destination_length, &attr);
1235 dbg(2, "Destlength=%d\n", attr.u.num);
1236 param[3].name=_("Destination Length");
1237 if ( attr.u.num >= 2000 )
1239 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1243 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1246 item_attr_get(item, attr_destination_time, &attr);
1247 dbg(2, "Desttime=%d\n", attr.u.num);
1249 param[4].name=_("Destination Time");
1252 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1256 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1258 datawindow_add(this_->roadbook_window, param, 5);
1260 map_rect_destroy(mr);
1262 datawindow_mode(this_->roadbook_window, 0);
1266 navit_window_roadbook_destroy(struct navit *this_)
1269 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1270 this_->roadbook_window=NULL;
1271 this_->roadbook_callback=NULL;
1274 navit_window_roadbook_new(struct navit *this_)
1276 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1280 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1281 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1282 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1283 navit_window_roadbook_update(this_);
1287 navit_init(struct navit *this_)
1292 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1295 navit_destroy(this_);
1299 dbg(0,"no graphics\n");
1300 navit_destroy(this_);
1303 dbg(2,"Connecting gui to graphics\n");
1304 if (gui_set_graphics(this_->gui, this_->gra)) {
1305 struct attr attr_type_gui, attr_type_graphics;
1306 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1307 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1308 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1309 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1310 dbg(0," for explanations and solutions\n");
1312 navit_destroy(this_);
1315 dbg(2,"Initializing graphics\n");
1316 dbg(2,"Setting Vehicle\n");
1317 navit_set_vehicle(this_, this_->vehicle);
1318 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1319 if (this_->mapsets) {
1320 ms=this_->mapsets->data;
1322 if ((map=route_get_map(this_->route)))
1323 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1324 if ((map=route_get_graph_map(this_->route))) {
1325 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1326 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1328 route_set_mapset(this_->route, ms);
1329 route_set_projection(this_->route, transform_get_projection(this_->trans));
1331 if (this_->tracking) {
1332 tracking_set_mapset(this_->tracking, ms);
1334 tracking_set_route(this_->tracking, this_->route);
1336 if (this_->navigation) {
1337 if ((map=navigation_get_map(this_->navigation))) {
1338 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1339 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1342 if (this_->tracking) {
1343 if ((map=tracking_get_map(this_->tracking))) {
1344 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1345 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1348 navit_add_bookmarks_from_file(this_);
1349 navit_add_former_destinations_from_file(this_);
1352 struct attr callback;
1353 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1354 callback.type=attr_callback;
1355 callback.u.callback=this_->route_cb;
1356 route_add_attr(this_->route, &callback);
1358 if (this_->navigation) {
1359 if (this_->speech) {
1360 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1361 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1364 navigation_set_route(this_->navigation, this_->route);
1366 dbg(2,"Setting Center\n");
1367 char *center_file = navit_get_center_file(FALSE);
1368 navit_set_center_from_file(this_, center_file);
1369 g_free(center_file);
1371 if (this_->menubar) {
1372 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1374 navit_add_menu_windows_items(this_, men);
1380 navit_window_roadbook_new(this_);
1381 navit_window_items_new(this_);
1384 messagelist_init(this_->messages);
1386 navit_set_cursors(this_);
1388 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1390 dbg(2,"ready=%d\n",this_->ready);
1391 if (this_->ready == 3)
1394 routech_test(this_);
1399 navit_zoom_to_route(struct navit *this_, int orientation)
1402 struct map_rect *mr=NULL;
1405 struct coord_rect r;
1406 int count=0,scale=16;
1410 map=route_get_map(this_->route);
1411 dbg(1,"map=%p\n",map);
1413 mr=map_rect_new(map, NULL);
1414 dbg(1,"mr=%p\n",mr);
1416 while ((item=map_rect_get_item(mr))) {
1417 dbg(1,"item=%s\n", item_to_name(item->type));
1418 while (item_coord_get(item, &c, 1)) {
1423 coord_rect_extend(&r, &c);
1430 c.x=(r.rl.x+r.lu.x)/2;
1431 c.y=(r.rl.y+r.lu.y)/2;
1432 dbg(1,"count=%d\n",count);
1433 if (orientation != -1)
1434 transform_set_yaw(this_->trans, orientation);
1435 transform_set_center(this_->trans, &c);
1436 dbg(1,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1437 while (scale < 1<<20) {
1439 transform_set_scale(this_->trans, scale);
1440 transform_setup_source_rect(this_->trans);
1441 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1442 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1443 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1444 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1445 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1451 if (this_->ready == 3)
1452 navit_draw_async(this_,0);
1456 navit_cmd_zoom_to_route(struct navit *this)
1458 navit_zoom_to_route(this, 0);
1463 * Change the current zoom level
1465 * @param navit The navit instance
1466 * @param center The point where to center the map, including its projection
1470 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1472 struct coord *c=transform_center(this_->trans);
1474 enum projection pro = transform_get_projection(this_->trans);
1475 if (pro != center->pro) {
1478 transform_from_to(&c1, center->pro, &c2, pro);
1485 navit_set_timeout(this_);
1486 if (this_->ready == 3)
1491 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1495 transform_set_center(this_->trans, c);
1496 transform_get_size(this_->trans, &width, &height);
1499 update_transformation(this_->trans, &po, p, NULL);
1501 navit_set_timeout(this_);
1505 * Links all vehicles to a cursor depending on the current profile.
1507 * @param this_ A navit instance
1508 * @author Ralph Sennhauser (10/2009)
1511 navit_set_cursors(struct navit *this_)
1514 struct navit_vehicle *nv;
1518 v=g_list_first(this_->vehicles); // GList of navit_vehicles
1521 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL))
1522 c=layout_get_cursor(this_->layout_current, name.u.str);
1524 c=layout_get_cursor(this_->layout_current, "default");
1525 vehicle_set_cursor(nv->vehicle, c);
1532 navit_get_cursor_pnt(struct navit *this_, struct point *p, int *dir)
1535 struct navit_vehicle *nv=this_->vehicle;
1537 float offset=30; // Cursor offset from the center of the screen (percent).
1538 #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 */
1539 float min_offset = 0.; // Percent offset at min_offset_speed.
1540 float max_offset = 30.; // Percent offset at max_offset_speed.
1541 int min_offset_speed = 2; // Speed in km/h
1542 int max_offset_speed = 50; // Speed ini km/h
1543 // Calculate cursor offset from the center of the screen, upon speed.
1544 if (nv->speed <= min_offset_speed) {
1545 offset = min_offset;
1546 } else if (nv->speed > max_offset_speed) {
1547 offset = max_offset;
1549 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1553 transform_get_size(this_->trans, &width, &height);
1554 if (this_->orientation == -1) {
1556 p->y=(50 + offset)*height/100;
1561 if (this_->tracking && this_->tracking_flag) {
1562 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1564 mdir=nv->dir-this_->orientation;
1567 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1568 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1570 *dir=this_->orientation;
1576 navit_set_center_cursor(struct navit *this_)
1580 struct navit_vehicle *nv=this_->vehicle;
1581 navit_get_cursor_pnt(this_, &pn, &dir);
1582 transform_set_yaw(this_->trans, dir);
1583 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1584 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1585 if (this_->ready == 3)
1586 navit_draw_async(this_, 1);
1590 navit_cmd_set_center_cursor(struct navit *this_)
1592 navit_set_center_cursor(this_);
1596 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1600 transform_reverse(this_->trans, p, &c);
1603 pc.pro = transform_get_projection(this_->trans);
1604 navit_set_center(this_, &pc, set_timeout);
1608 switch((*attrs)->type) {
1610 zoom=(*attrs)->u.num;
1613 g=*((*attrs)->u.coord_geo);
1618 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1620 int dir=0, orient_old=0, attr_updated=0;
1624 struct navit_vehicle *nv;
1625 struct attr active=(struct attr){attr_active,{(void *)0}};
1627 switch (attr->type) {
1629 attr_updated=(this_->autozoom_secs != attr->u.num);
1630 this_->autozoom_secs = attr->u.num;
1632 case attr_autozoom_active:
1633 attr_updated=(this_->autozoom_active != attr->u.num);
1634 this_->autozoom_active = attr->u.num;
1637 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1638 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1639 transform_set_center(this_->trans, &co);
1641 case attr_drag_bitmap:
1642 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1643 this_->drag_bitmap=!!attr->u.num;
1645 case attr_flags_graphics:
1646 attr_updated=(this_->graphics_flags != attr->u.num);
1647 this_->graphics_flags=attr->u.num;
1650 if (!this_->vehicle)
1652 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1653 this_->vehicle->follow_curr = attr->u.num;
1656 if(this_->layout_current!=attr->u.layout) {
1657 this_->layout_current=attr->u.layout;
1658 graphics_font_destroy_all(this_->gra);
1659 navit_set_cursors(this_);
1660 if (this_->ready == 3)
1665 case attr_orientation:
1666 orient_old=this_->orientation;
1667 this_->orientation=attr->u.num;
1669 if (this_->orientation != -1) {
1670 dir = this_->orientation;
1672 if (this_->vehicle) {
1673 dir = this_->vehicle->dir;
1676 transform_set_yaw(this_->trans, dir);
1677 if (orient_old != this_->orientation) {
1679 if (this_->ready == 3)
1686 case attr_osd_configuration:
1687 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1688 attr_updated=(this_->osd_configuration != attr->u.num);
1689 this_->osd_configuration=attr->u.num;
1692 attr_updated=(this_->pitch != attr->u.num);
1693 this_->pitch=attr->u.num;
1694 transform_set_pitch(this_->trans, this_->pitch);
1695 if (!init && attr_updated && this_->ready == 3)
1698 case attr_projection:
1699 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1700 navit_projection_set(this_, attr->u.projection);
1704 case attr_recent_dest:
1705 attr_updated=(this_->recentdest_count != attr->u.num);
1706 this_->recentdest_count=attr->u.num;
1709 if(this_->speech && this_->speech != attr->u.speech) {
1711 this_->speech = attr->u.speech;
1715 attr_updated=(this_->center_timeout != attr->u.num);
1716 this_->center_timeout = attr->u.num;
1719 attr_updated=(this_->tracking_flag != !!attr->u.num);
1720 this_->tracking_flag=!!attr->u.num;
1722 case attr_use_mousewheel:
1723 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1724 this_->use_mousewheel=!!attr->u.num;
1730 if (nv->vehicle == attr->u.vehicle) {
1731 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1733 vehicle_set_attr(this_->vehicle->vehicle, &active);
1735 vehicle_set_attr(nv->vehicle, &active);
1738 navit_set_vehicle(this_, nv);
1744 zoom=transform_get_scale(this_->trans);
1745 attr_updated=(zoom != attr->u.num);
1746 transform_set_scale(this_->trans, attr->u.num);
1747 if (attr_updated && !init)
1751 navit_add_message(this_, attr->u.str);
1753 case attr_follow_cursor:
1754 attr_updated=(this_->follow_cursor != !!attr->u.num);
1755 this_->follow_cursor=!!attr->u.num;
1760 if (attr_updated && !init) {
1761 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1762 if (attr->type == attr_osd_configuration)
1763 graphics_draw_mode(this_->gra, draw_mode_end);
1769 navit_set_attr(struct navit *this_, struct attr *attr)
1771 return navit_set_attr_do(this_, attr, 0);
1775 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1777 struct message *msg;
1783 msg = navit_get_messages(this_);
1791 len += strlen(msg->text) + 1;
1794 attr->u.str = g_malloc(len + 1);
1796 msg = navit_get_messages(this_);
1799 g_stpcpy((attr->u.str + offset), msg->text);
1800 offset += strlen(msg->text);
1801 attr->u.str[offset] = '\n';
1807 attr->u.str[len] = '\0';
1809 case attr_bookmark_map:
1810 attr->u.map=this_->bookmark;
1812 case attr_callback_list:
1813 attr->u.callback_list=this_->attr_cbl;
1815 case attr_destination:
1816 if (! this_->destination_valid)
1818 attr->u.pcoord=&this_->destination;
1820 case attr_displaylist:
1821 attr->u.displaylist=this_->displaylist;
1822 return (attr->u.displaylist != NULL);
1823 case attr_former_destination_map:
1824 attr->u.map=this_->former_destination;
1827 attr->u.graphics=this_->gra;
1828 ret=(attr->u.graphics != NULL);
1831 attr->u.gui=this_->gui;
1832 ret=(attr->u.gui != NULL);
1837 iter->u.list=g_list_next(iter->u.list);
1839 iter->u.list=this_->layouts;
1843 attr->u.layout=(struct layout *)iter->u.list->data;
1845 attr->u.layout=this_->layout_current;
1849 if (iter && this_->mapsets) {
1850 if (!iter->u.mapset_handle) {
1851 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1853 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1855 mapset_close(iter->u.mapset_handle);
1863 attr->u.mapset=this_->mapsets->data;
1864 return (attr->u.mapset != NULL);
1865 case attr_navigation:
1866 attr->u.navigation=this_->navigation;
1868 case attr_orientation:
1869 attr->u.num=this_->orientation;
1871 case attr_osd_configuration:
1872 attr->u.num=this_->osd_configuration;
1875 attr->u.num=transform_get_pitch(this_->trans);
1877 case attr_projection:
1879 attr->u.num=transform_get_projection(this_->trans);
1885 attr->u.route=this_->route;
1888 attr->u.speech=this_->speech;
1891 attr->u.num=this_->tracking_flag;
1893 case attr_transformation:
1894 attr->u.transformation=this_->trans;
1899 iter->u.list=g_list_next(iter->u.list);
1901 iter->u.list=this_->vehicles;
1905 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1907 if(this_->vehicle) {
1908 attr->u.vehicle=this_->vehicle->vehicle;
1915 attr->u.num=transform_get_scale(this_->trans);
1917 case attr_autozoom_active:
1918 attr->u.num=this_->autozoom_active;
1920 case attr_follow_cursor:
1921 attr->u.num=this_->follow_cursor;
1931 navit_add_log(struct navit *this_, struct log *log)
1933 struct attr type_attr;
1934 if (!log_get_attr(log, attr_type, &type_attr, NULL))
1936 if (!strcmp(type_attr.u.str, "textfile_debug")) {
1937 char *header = "type=track_tracked\n";
1938 if (this_->textfile_debug_log)
1940 log_set_header(log, header, strlen(header));
1941 this_->textfile_debug_log=log;
1948 navit_add_attr(struct navit *this_, struct attr *attr)
1951 switch (attr->type) {
1953 navit_add_callback(this_, attr->u.callback);
1956 ret=navit_add_log(this_, attr->u.log);
1959 ret=navit_set_gui(this_, attr->u.gui);
1962 ret=navit_set_graphics(this_, attr->u.graphics);
1965 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1966 if(!this_->layout_current)
1967 this_->layout_current=attr->u.layout;
1970 this_->route=attr->u.route;
1973 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1975 case attr_navigation:
1976 this_->navigation=attr->u.navigation;
1978 case attr_recent_dest:
1979 this_->recentdest_count = attr->u.num;
1982 this_->speech=attr->u.speech;
1985 this_->tracking=attr->u.tracking;
1988 ret=navit_add_vehicle(this_, attr->u.vehicle);
1990 case attr_vehicleprofile:
1991 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
1993 case attr_autozoom_min:
1994 this_->autozoom_min = attr->u.num;
1999 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2004 navit_remove_attr(struct navit *this_, struct attr *attr)
2007 switch (attr->type) {
2009 navit_remove_callback(this_, attr->u.callback);
2018 navit_attr_iter_new(void)
2020 return g_new0(struct attr_iter, 1);
2024 navit_attr_iter_destroy(struct attr_iter *iter)
2030 navit_add_callback(struct navit *this_, struct callback *cb)
2032 callback_list_add(this_->attr_cbl, cb);
2036 navit_remove_callback(struct navit *this_, struct callback *cb)
2038 callback_list_remove(this_->attr_cbl, cb);
2042 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2044 * @param navit The navit instance
2049 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2051 struct point cursor_pnt;
2052 enum projection pro;
2059 pro=transform_get_projection(this_->trans);
2060 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2062 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
2067 pro=transform_get_projection(this_->trans);
2068 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2071 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2073 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2079 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2081 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2082 struct pcoord cursor_pc;
2083 struct point cursor_pnt, *pnt=&cursor_pnt;
2084 struct tracking *tracking=NULL;
2085 enum projection pro=transform_get_projection(this_->trans);
2087 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2091 if (this_->ready != 3) {
2092 profile(0,"return 1\n");
2095 navit_layout_switch(this_);
2096 if (this_->vehicle == nv && this_->tracking_flag)
2097 tracking=this_->tracking;
2099 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2100 attr_object=tracking;
2101 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2103 attr_object=nv->vehicle;
2104 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2106 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2107 if (!attr_valid.u.num != attr_position_valid_invalid)
2109 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2110 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2111 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2112 profile(0,"return 2\n");
2115 nv->dir=*attr_dir.u.numd;
2116 nv->speed=*attr_speed.u.numd;
2117 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2118 if (nv != this_->vehicle) {
2119 navit_vehicle_draw(this_, nv, NULL);
2120 profile(0,"return 3\n");
2123 cursor_pc.x = nv->coord.x;
2124 cursor_pc.y = nv->coord.y;
2125 cursor_pc.pro = pro;
2128 route_set_position_from_tracking(this_->route, tracking, pro);
2130 route_set_position(this_->route, &cursor_pc);
2132 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2133 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2134 if (this_->gui && nv->speed > 2)
2135 gui_disable_suspend(this_->gui);
2137 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2138 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2139 (nv->follow_curr == 1 || !transform_within_border(this_->trans, &cursor_pnt, border)))
2140 navit_set_center_cursor(this_);
2142 navit_vehicle_draw(this_, nv, pnt);
2144 if (nv->follow_curr > 1)
2147 nv->follow_curr=nv->follow;
2148 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2150 /* Finally, if we reached our destination, stop navigation. */
2151 if (this_->route && route_destination_reached(this_->route)) {
2152 navit_set_destination(this_, NULL, NULL, 0);
2154 profile(0,"return 5\n");
2158 * Set the position of the vehicle
2160 * @param navit The navit instance
2161 * @param c The coordinate to set as position
2166 navit_set_position(struct navit *this_, struct pcoord *c)
2169 route_set_position(this_->route, c);
2170 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2172 if (this_->ready == 3)
2177 navit_set_vehicleprofile(struct navit *this_, char *name)
2181 l=this_->vehicleprofiles;
2183 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2184 if (!strcmp(attr.u.str, name)) {
2185 this_->vehicleprofile=l->data;
2187 route_set_profile(this_->route, this_->vehicleprofile);
2197 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2201 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2202 if (navit_set_vehicleprofile(this_, attr.u.str))
2205 navit_set_vehicleprofile(this_,"car");
2209 * Register a new vehicle
2211 * @param navit The navit instance
2212 * @param v The vehicle instance
2213 * @returns 1 for success
2216 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2218 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2219 struct attr follow, active, animate;
2224 nv->animate_cursor=0;
2225 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2226 nv->follow=nv->follow=follow.u.num;
2227 nv->follow_curr=nv->follow;
2228 this_->vehicles=g_list_append(this_->vehicles, nv);
2229 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2230 navit_set_vehicle(this_, nv);
2231 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2232 nv->animate_cursor=animate.u.num;
2233 nv->callback.type=attr_callback;
2234 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2235 vehicle_add_attr(nv->vehicle, &nv->callback);
2236 vehicle_set_attr(nv->vehicle, &this_->self);
2244 navit_get_gui(struct navit *this_)
2249 struct transformation *
2250 navit_get_trans(struct navit *this_)
2252 return this_->trans;
2256 navit_get_route(struct navit *this_)
2258 return this_->route;
2262 navit_get_navigation(struct navit *this_)
2264 return this_->navigation;
2267 struct displaylist *
2268 navit_get_displaylist(struct navit *this_)
2270 return this_->displaylist;
2274 navit_layout_switch(struct navit *n)
2278 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2279 double trise,tset,trise_actual;
2281 int year, month, day;
2283 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2284 return; //No layout - nothing to switch
2286 l=layout_attr.u.layout;
2288 if (l->dayname || l->nightname) {
2289 //Ok, we know that we have profile to switch
2291 //Check that we aren't calculating too fast
2292 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2293 currTs=iso8601_to_secs(iso8601_attr.u.str);
2295 if (currTs-(n->prevTs)<60) {
2296 //We've have to wait a little
2299 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2301 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2302 //No position - no sun
2305 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2306 return; //No valid fix yet
2309 //We calculate sunrise anyway, cause it is need both for day and for night
2310 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,35,1,&trise,&tset)!=0) {
2311 //near the pole sun never rises/sets, so we should never switch profiles
2320 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2321 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2322 //The sun is rising now!
2323 if (strcmp(l->name,l->dayname)) {
2324 navit_set_layout_by_name(n,l->dayname);
2329 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lat,geo_attr.u.coord_geo->lng,-12,0,&trise,&tset)!=0) {
2330 //near the pole sun never rises/sets, so we should never switch profiles
2335 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2336 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
2337 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2339 if (strcmp(l->name,l->nightname)) {
2340 navit_set_layout_by_name(n,l->nightname);
2350 navit_set_layout_by_name(struct navit *n,char *name)
2353 struct attr_iter iter;
2354 struct attr layout_attr;
2358 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2359 return 0; //No layouts - nothing to do
2361 if (iter.u.list==NULL) {
2365 iter.u.list=g_list_first(iter.u.list);
2367 while(iter.u.list) {
2368 l=(struct layout*)iter.u.list->data;
2369 if (!strcmp(name,l->name)) {
2370 layout_attr.u.layout=l;
2371 layout_attr.type=attr_layout;
2372 navit_set_attr(n,&layout_attr);
2373 iter.u.list=g_list_first(iter.u.list);
2376 iter.u.list=g_list_next(iter.u.list);
2379 iter.u.list=g_list_first(iter.u.list);
2384 navit_block(struct navit *this_, int block)
2387 this_->blocked |= 1;
2388 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2389 this_->blocked |= 2;
2392 if (this_->blocked & 2) {
2402 navit_destroy(struct navit *this_)
2404 /* TODO: destroy objects contained in this_ */
2406 vehicle_destroy(this_->vehicle->vehicle);
2407 char *center_file = navit_get_center_file(TRUE);
2408 navit_write_center_to_file(this_, center_file);
2409 g_free(center_file);
2410 callback_destroy(this_->nav_speech_cb);
2411 callback_destroy(this_->roadbook_callback);
2412 callback_destroy(this_->popup_callback);
2413 callback_destroy(this_->motion_timeout_callback);
2415 graphics_remove_callback(this_->gra, this_->resize_callback);
2416 callback_destroy(this_->resize_callback);
2418 graphics_remove_callback(this_->gra, this_->button_callback);
2419 callback_destroy(this_->button_callback);
2421 graphics_remove_callback(this_->gra, this_->motion_callback);
2422 callback_destroy(this_->motion_callback);