2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 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"
47 #include "data_window.h"
49 #include "navigation.h"
60 #include "navit_nls.h"
63 * @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
67 //! The navit_vehicule
68 struct navit_vehicle {
70 /*! Limit of the update counter. See navit_add_vehicle */
72 /*! Deprecated : Update counter itself. When it reaches 'update' counts, route is updated */
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 */
83 struct cursor *cursor;
84 struct vehicle *vehicle;
94 struct layout *layout_current;
96 struct action *action;
97 struct transformation *trans;
98 struct compass *compass;
100 struct navigation *navigation;
101 struct speech *speech;
102 struct tracking *tracking;
105 struct displaylist *displaylist;
109 int recentdest_count;
111 GList *windows_items;
112 struct navit_vehicle *vehicle;
113 struct callback_list *attr_cbl;
115 struct callback *nav_speech_cb;
116 struct callback *roadbook_callback;
117 struct callback *popup_callback;
118 struct datawindow *roadbook_window;
119 struct map *bookmark;
120 struct map *former_destination;
121 GHashTable *bookmarks_hash;
122 struct point pressed, last, current;
123 int button_pressed,moved,popped,zoomed;
126 struct event_timeout *button_timeout, *motion_timeout;
127 struct callback *motion_timeout_callback;
129 int ignore_graphics_events;
130 struct log *textfile_debug_log;
131 struct pcoord destination;
132 int destination_valid;
137 GHashTable *commands;
138 struct callback *resize_callback,*button_callback,*motion_callback;
141 struct gui *main_loop_gui;
146 struct mapset_handle *mapset_handle;
150 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
151 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
152 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
155 navit_add_mapset(struct navit *this_, struct mapset *ms)
157 this_->mapsets = g_list_append(this_->mapsets, ms);
161 navit_get_mapset(struct navit *this_)
164 return this_->mapsets->data;
166 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
172 navit_get_tracking(struct navit *this_)
174 return this_->tracking;
178 navit_draw(struct navit *this_)
181 struct navit_vehicle *nv;
183 if (this_->blocked) {
187 transform_setup_source_rect(this_->trans);
191 navit_vehicle_draw(this_, nv, NULL);
194 graphics_draw(this_->gra, this_->displaylist, this_->mapsets, this_->trans, this_->layout_current);
198 for (i = 0 ; i < 500 ; i++)
199 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 1);
206 navit_draw_displaylist(struct navit *this_)
208 if (this_->ready == 3)
209 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 1);
213 navit_handle_resize(struct navit *this_, int w, int h)
215 struct map_selection sel;
216 memset(&sel, 0, sizeof(sel));
221 transform_set_screen_selection(this_->trans, &sel);
223 graphics_set_rect(this_->gra, &sel.u.p_rect);
224 if (this_->ready == 3)
229 navit_resize(void *data, int w, int h)
231 struct navit *this=data;
232 if (!this->ignore_graphics_events)
233 navit_handle_resize(this, w, h);
237 navit_get_width(struct navit *this_)
244 navit_get_height(struct navit *this_)
250 navit_popup(void *data)
252 struct navit *this_=data;
253 popup(this_, 1, &this_->pressed);
254 this_->button_timeout=NULL;
260 navit_ignore_button(struct navit *this_)
262 this_->ignore_button=1;
266 navit_ignore_graphics_events(struct navit *this_, int ignore)
268 this_->ignore_graphics_events=ignore;
272 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
277 double angleo,anglen;
279 transform_reverse(tr, old, &co);
281 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
282 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
283 yaw=transform_get_yaw(tr)+angleo-anglen;
284 transform_set_yaw(tr, yaw % 360);
286 transform_reverse(tr, new, &cn);
287 cp=transform_get_center(tr);
290 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
291 transform_set_center(tr, &c);
295 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
299 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, (void *)pressed, (void *)button, p);
300 if (this_->ignore_button) {
301 this_->ignore_button=0;
309 this_->button_pressed=1;
313 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
316 navit_set_center_screen(this_, p);
318 popup(this_, button, p);
319 if (button == 4 && this_->use_mousewheel) {
321 navit_zoom_in(this_, 2, p);
323 if (button == 5 && this_->use_mousewheel) {
325 navit_zoom_out(this_, 2, p);
328 this_->button_pressed=0;
329 if (this_->button_timeout) {
330 event_remove_timeout(this_->button_timeout);
331 this_->button_timeout=NULL;
332 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
333 if (!this_->zoomed) {
334 this_->last_moved = time(NULL);
336 navit_set_center_screen(this_, p);
339 if (this_->motion_timeout) {
340 event_remove_timeout(this_->motion_timeout);
341 this_->motion_timeout=NULL;
348 update_transformation(this_->trans, &this_->pressed, p, &pr);
350 update_transformation(this_->trans, &this_->pressed, p, NULL);
352 graphics_draw_drag(this_->gra, NULL);
353 graphics_overlay_disable(this_->gra, 0);
354 if (!this_->zoomed) {
355 this_->last_moved = time(NULL);
365 navit_button(void *data, int pressed, int button, struct point *p)
367 struct navit *this=data;
368 if (!this->ignore_graphics_events) {
369 if (! this->popup_callback)
370 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
371 navit_handle_button(this, pressed, button, p, this->popup_callback);
377 navit_motion_timeout(struct navit *this_)
381 if (this_->drag_bitmap) {
383 point.x=(this_->current.x-this_->pressed.x);
384 point.y=(this_->current.y-this_->pressed.y);
385 if (graphics_draw_drag(this_->gra, &point)) {
386 graphics_overlay_disable(this_->gra, 1);
387 graphics_draw_mode(this_->gra, draw_mode_end);
389 this_->motion_timeout=NULL;
393 dx=(this_->current.x-this_->last.x);
394 dy=(this_->current.y-this_->last.y);
396 struct transformation *tr;
398 this_->last=this_->current;
399 graphics_overlay_disable(this_->gra, 1);
400 tr=transform_dup(this_->trans);
404 update_transformation(tr, &this_->pressed, &this_->current, &pr);
406 update_transformation(tr, &this_->pressed, &this_->current, NULL);
409 graphics_displaylist_move(this_->displaylist, dx, dy);
411 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, 0);
412 transform_destroy(tr);
415 this_->motion_timeout=NULL;
420 navit_handle_motion(struct navit *this_, struct point *p)
424 if (this_->button_pressed && !this_->popped) {
425 dx=(p->x-this_->pressed.x);
426 dy=(p->y-this_->pressed.y);
427 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
428 if (this_->button_timeout) {
429 event_remove_timeout(this_->button_timeout);
430 this_->button_timeout=NULL;
433 if (! this_->motion_timeout_callback)
434 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
435 if (! this_->motion_timeout)
436 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
442 navit_motion(void *data, struct point *p)
444 struct navit *this=data;
445 if (!this->ignore_graphics_events)
446 navit_handle_motion(this, p);
450 navit_scale(struct navit *this_, long scale, struct point *p)
452 struct coord c1, c2, *center;
454 transform_reverse(this_->trans, p, &c1);
455 transform_set_scale(this_->trans, scale);
457 transform_reverse(this_->trans, p, &c2);
458 center = transform_center(this_->trans);
459 center->x += c1.x - c2.x;
460 center->y += c1.y - c2.y;
466 * Change the current zoom level, zooming closer to the ground
468 * @param navit The navit instance
469 * @param factor The zoom factor, usually 2
470 * @param p The invariant point (if set to NULL, default to center)
474 navit_zoom_in(struct navit *this_, int factor, struct point *p)
476 long scale=transform_get_scale(this_->trans)/factor;
479 navit_scale(this_, scale, p);
483 * Change the current zoom level
485 * @param navit The navit instance
486 * @param factor The zoom factor, usually 2
487 * @param p The invariant point (if set to NULL, default to center)
491 navit_zoom_out(struct navit *this_, int factor, struct point *p)
493 long scale=transform_get_scale(this_->trans)*factor;
494 navit_scale(this_, scale, p);
498 navit_new(struct attr *parent, struct attr **attrs)
500 struct navit *this_=g_new0(struct navit, 1);
501 struct pcoord center;
504 enum projection pro=projection_mg;
510 this_->self.type=attr_navit;
511 this_->self.u.navit=this_;
512 this_->attr_cbl=callback_list_new();
513 main_add_navit(this_);
515 #if !defined(_WIN32) && !defined(__CEGCC__)
516 f=popen("pidof /usr/bin/ipaq-sleep","r");
518 fscanf(f,"%d",&this_->pid);
519 dbg(1,"ipaq_sleep pid=%d\n", this_->pid);
524 this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
526 this_->cursor_flag=1;
527 this_->orientation=-1;
528 this_->tracking_flag=1;
529 this_->recentdest_count=10;
531 this_->last_moved = 0;
532 this_->center_timeout = 10;
533 this_->use_mousewheel = 1;
535 for (;*attrs; attrs++) {
536 switch((*attrs)->type) {
538 zoom=(*attrs)->u.num;
541 g=*((*attrs)->u.coord_geo);
544 this_->cursor_flag=!!(*attrs)->u.num;
546 case attr_orientation:
547 this_->orientation=(*attrs)->u.num;
550 this_->tracking_flag=!!(*attrs)->u.num;
552 case attr_recent_dest:
553 this_->recentdest_count=(*attrs)->u.num;
555 case attr_drag_bitmap:
556 this_->drag_bitmap=!!(*attrs)->u.num;
558 case attr_use_mousewheel:
559 this_->use_mousewheel=!!(*attrs)->u.num;
562 this_->center_timeout = (*attrs)->u.num;
565 dbg(0, "Unexpected attribute %x\n",(*attrs)->type);
569 transform_from_geo(pro, &g, &co);
574 this_->trans=transform_new();
575 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
576 this_->displaylist=graphics_displaylist_new();
577 this_->commands=g_hash_table_new(g_str_hash, g_str_equal);
578 navit_command_register(this_, "zoom_in", callback_new_3(callback_cast(navit_zoom_in), this_, (void *)2, NULL));
579 navit_command_register(this_, "zoom_out", callback_new_3(callback_cast(navit_zoom_out), this_, (void *)2, NULL));
584 navit_set_gui(struct navit *this_, struct gui *gui)
589 if (gui_has_main_loop(this_->gui)) {
590 if (! main_loop_gui) {
591 main_loop_gui=this_->gui;
593 dbg(0,"gui with main loop already active, ignoring this instance");
601 navit_set_graphics(struct navit *this_, struct graphics *gra)
606 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
607 graphics_add_callback(gra, this_->resize_callback);
608 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
609 graphics_add_callback(gra, this_->button_callback);
610 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
611 graphics_add_callback(gra, this_->motion_callback);
616 navit_get_graphics(struct navit *this_)
622 navit_projection_set(struct navit *this_, enum projection pro)
627 c=transform_center(this_->trans);
628 transform_to_geo(transform_get_projection(this_->trans), c, &g);
629 transform_set_projection(this_->trans, pro);
630 transform_from_geo(pro, &g, c);
635 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
638 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
653 while ((ch = fgetc(f)) != EOF) {
654 if ((ch == '\n') && (prev != '\n')) {
660 if (prev != '\n') { // Last line did not end with a newline
665 f = fopen(file, "r+");
667 while (lines >= limit) { // We have to "scroll up"
669 numc = 0; // Counts how many bytes we have in our line to scroll up
670 while ((ch = fgetc(f)) != EOF) {
677 buffer=g_malloc(numc);
678 offset = numc; // Offset holds where we currently are
681 fseek(f,offset,SEEK_SET);
682 readc = fread(buffer,1,numc,f);
684 fseek(f,-(numc+readc),SEEK_CUR);
685 fwrite(buffer,1,readc,f);
688 } while (readc == numc);
692 ftruncate(fd,(offset-numc));
706 prostr = projection_to_name(c->pro);
707 fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
708 prostr, *prostr ? ":" : "",
709 c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
710 c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y,
719 * navit_get_user_data_directory
721 * returns the directory used to store user data files (center.txt,
722 * destination.txt, bookmark.txt, ...)
724 * arg: gboolean create: create the directory if it does not exist
727 navit_get_user_data_directory(gboolean create) {
729 dir = getenv("NAVIT_USER_DATADIR");
730 if (create && !file_exists(dir)) {
731 dbg(0,"creating dir %s\n", dir);
732 if (file_mkdir(dir,0)) {
733 dbg(0,"failed creating dir %s\n", dir);
742 * navit_get_destination_file
744 * returns the name of the file used to store destinations with its
747 * arg: gboolean create: create the directory where the file is stored
748 * if it does not exist
751 navit_get_destination_file(gboolean create)
753 return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
757 * navit_get_bookmark_file
759 * returns the name of the file used to store bookmarks with its
762 * arg: gboolean create: create the directory where the file is stored
763 * if it does not exist
766 navit_get_bookmark_file(gboolean create)
768 return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
773 * navit_get_bookmark_file
775 * returns the name of the file used to store the center file with its
778 * arg: gboolean create: create the directory where the file is stored
779 * if it does not exist
782 navit_get_center_file(gboolean create)
784 return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
788 navit_set_center_from_file(struct navit *this_, char *file)
793 size_t line_size = 0;
795 struct coord *center;
797 f = fopen(file, "r");
800 getline(&line, &line_size, f);
803 center = transform_center(this_->trans);
804 pro = transform_get_projection(this_->trans);
805 coord_parse(g_strchomp(line), pro, center);
812 navit_write_center_to_file(struct navit *this_, char *file)
816 struct coord *center;
818 f = fopen(file, "w+");
820 center = transform_center(this_->trans);
821 pro = transform_get_projection(this_->trans);
822 coord_print(pro, center, f);
832 * Start the route computing to a given set of coordinates
834 * @param navit The navit instance
835 * @param c The coordinate to start routing to
836 * @param description A label which allows the user to later identify this destination in the former destinations selection
840 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description)
843 this_->destination=*c;
844 this_->destination_valid=1;
846 this_->destination_valid=0;
847 char *destination_file = navit_get_destination_file(TRUE);
848 navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
849 g_free(destination_file);
850 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
852 route_set_destination(this_->route, c);
853 if (this_->navigation) {
854 navigation_flush(this_->navigation);
855 navigation_update(this_->navigation, this_->route);
858 if (this_->ready == 3)
864 * @brief Checks if a route is calculated
866 * This function checks if a route is calculated.
868 * @param this_ The navit struct whose route should be checked.
869 * @return True if the route is set, false otherwise.
872 navit_check_route(struct navit *this_)
875 return route_get_path_set(this_->route);
882 * Record the given set of coordinates as a bookmark
884 * @param navit The navit instance
885 * @param c The coordinate to store
886 * @param description A label which allows the user to later identify this bookmark
890 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
892 char *bookmark_file = navit_get_bookmark_file(TRUE);
893 navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
894 g_free(bookmark_file);
896 callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
899 struct navit *global_navit;
902 navit_add_bookmarks_from_file(struct navit *this_)
904 char *bookmark_file = navit_get_bookmark_file(FALSE);
905 struct attr parent={attr_navit, .u.navit=this_};
906 struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
907 struct attr *attrs[]={&type, &data, NULL};
909 this_->bookmark=map_new(&parent, attrs);
910 g_free(bookmark_file);
914 navit_former_destinations_active(struct navit *this_)
916 char *destination_file = navit_get_destination_file(FALSE);
920 f=fopen(destination_file,"r");
922 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
926 g_free(destination_file);
931 navit_add_former_destinations_from_file(struct navit *this_)
933 char *destination_file = navit_get_destination_file(FALSE);
934 struct attr parent={attr_navit, .u.navit=this_};
935 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
936 struct attr *attrs[]={&type, &data, NULL};
943 this_->former_destination=map_new(&parent, attrs);
944 g_free(destination_file);
945 if (!this_->route || !navit_former_destinations_active(this_))
947 mr=map_rect_new(this_->former_destination, NULL);
948 while ((item=map_rect_get_item(mr))) {
949 if (item->type == type_former_destination && item_coord_get(item, &c, 1))
952 map_rect_destroy(mr);
953 pc.pro=map_projection(this_->former_destination);
957 route_set_destination(this_->route, &pc);
958 this_->destination=pc;
959 this_->destination_valid=1;
965 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
970 if (this_->textfile_debug_log && this_->vehicle) {
971 str1=g_strdup_vprintf(fmt, ap);
972 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
973 log_write(this_->textfile_debug_log, str2, strlen(str2));
981 navit_speech_estimate(struct navit *this_, char *str)
983 return speech_estimate_duration(this_->speech, str);
987 navit_say(struct navit *this_, char *text)
989 speech_say(this_->speech, text);
993 navit_speak(struct navit *this_)
995 struct navigation *nav=this_->navigation;
996 struct map *map=NULL;
997 struct map_rect *mr=NULL;
1001 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1003 dbg(1, "this_.speech->active %i\n", attr.u.num);
1008 map=navigation_get_map(nav);
1010 mr=map_rect_new(map, NULL);
1012 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1013 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1014 speech_say(this_->speech, attr.u.str);
1015 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1017 map_rect_destroy(mr);
1022 navit_window_roadbook_update(struct navit *this_)
1024 struct navigation *nav=this_->navigation;
1025 struct map *map=NULL;
1026 struct map_rect *mr=NULL;
1029 struct param_list param[5];
1033 datawindow_mode(this_->roadbook_window, 1);
1035 map=navigation_get_map(nav);
1037 mr=map_rect_new(map, NULL);
1038 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1040 dbg(0,"while loop\n");
1041 while ((item=map_rect_get_item(mr))) {
1042 dbg(0,"item=%p\n", item);
1044 if (item->type != type_nav_position) {
1045 item_attr_get(item, attr_navigation_long, &attr);
1046 dbg(2, "Command='%s'\n", attr.u.str);
1047 param[0].value=g_strdup(attr.u.str);
1049 param[0].value=_("Position");
1050 param[0].name=_("Command");
1052 item_attr_get(item, attr_length, &attr);
1053 dbg(2, "Length=%d\n", attr.u.num);
1054 param[1].name=_("Length");
1056 if ( attr.u.num >= 2000 )
1058 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1062 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1065 item_attr_get(item, attr_time, &attr);
1066 dbg(2, "Time=%d\n", attr.u.num);
1068 param[2].name=_("Time");
1071 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1075 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1078 item_attr_get(item, attr_destination_length, &attr);
1079 dbg(2, "Destlength=%d\n", attr.u.num);
1080 param[3].name=_("Destination Length");
1081 if ( attr.u.num >= 2000 )
1083 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1087 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1090 item_attr_get(item, attr_destination_time, &attr);
1091 dbg(2, "Desttime=%d\n", attr.u.num);
1093 param[4].name=_("Destination Time");
1096 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1100 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1102 datawindow_add(this_->roadbook_window, param, 5);
1104 map_rect_destroy(mr);
1106 datawindow_mode(this_->roadbook_window, 0);
1110 navit_window_roadbook_destroy(struct navit *this_)
1113 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1114 this_->roadbook_window=NULL;
1115 this_->roadbook_callback=NULL;
1118 navit_window_roadbook_new(struct navit *this_)
1120 if (this_->roadbook_callback || this_->roadbook_window) {
1124 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1125 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1126 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1127 navit_window_roadbook_update(this_);
1131 navit_init(struct navit *this_)
1136 struct navit_vehicle *nv;
1140 navit_destroy(this_);
1144 dbg(0,"no graphics\n");
1145 navit_destroy(this_);
1148 if (gui_set_graphics(this_->gui, this_->gra)) {
1149 struct attr attr_type_gui, attr_type_graphics;
1150 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1151 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1152 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1153 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1154 dbg(0," for explanations and solutions\n");
1156 navit_destroy(this_);
1159 graphics_init(this_->gra);
1163 dbg(1,"parsed one vehicle\n");
1165 nv->callback.type=attr_callback;
1166 nv->callback.u.callback=callback_new_2(callback_cast(navit_vehicle_update), this_, nv);
1167 vehicle_add_attr(nv->vehicle, &nv->callback);
1168 vehicle_set_attr(nv->vehicle, &this_->self, NULL);
1172 if (this_->mapsets) {
1173 ms=this_->mapsets->data;
1175 if ((map=route_get_map(this_->route)))
1176 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1177 if ((map=route_get_graph_map(this_->route))) {
1178 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1179 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1181 route_set_mapset(this_->route, ms);
1182 route_set_projection(this_->route, transform_get_projection(this_->trans));
1184 if (this_->tracking) {
1185 tracking_set_mapset(this_->tracking, ms);
1187 tracking_set_route(this_->tracking, this_->route);
1189 if (this_->navigation) {
1190 if ((map=navigation_get_map(this_->navigation))) {
1191 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1192 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1195 if (this_->tracking) {
1196 if ((map=tracking_get_map(this_->tracking))) {
1197 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1198 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1201 navit_add_bookmarks_from_file(this_);
1202 navit_add_former_destinations_from_file(this_);
1204 if (this_->navigation && this_->speech) {
1205 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1206 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1208 char *center_file = navit_get_center_file(FALSE);
1209 navit_set_center_from_file(this_, center_file);
1210 g_free(center_file);
1212 if (this_->menubar) {
1213 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1215 navit_add_menu_windows_items(this_, men);
1221 navit_window_roadbook_new(this_);
1222 navit_window_items_new(this_);
1224 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1226 if (this_->ready == 3)
1231 navit_zoom_to_route(struct navit *this_)
1234 struct map_rect *mr;
1237 struct coord_rect r;
1238 int count=0,scale=16;
1241 map=route_get_map(this_->route);
1244 mr=map_rect_new(map, NULL);
1247 while ((item=map_rect_get_item(mr))) {
1248 while (item_coord_get(item, &c, 1)) {
1252 coord_rect_extend(&r, &c);
1258 c.x=(r.rl.x+r.lu.x)/2;
1259 c.y=(r.rl.y+r.lu.y)/2;
1260 dbg(0,"count=%d\n",count);
1261 ct=transform_center(this_->trans);
1263 dbg(0,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1264 while (scale < 1<<20) {
1266 transform_set_scale(this_->trans, scale);
1267 transform_setup_source_rect(this_->trans);
1268 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0, 0, NULL);
1269 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0, 0, NULL);
1270 dbg(0,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1271 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1272 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1278 if (this_->ready == 3)
1283 * Change the current zoom level
1285 * @param navit The navit instance
1286 * @param center The point where to center the map, including its projection
1290 navit_set_center(struct navit *this_, struct pcoord *center)
1292 struct coord *c=transform_center(this_->trans);
1294 enum projection pro = transform_get_projection(this_->trans);
1295 if (pro != center->pro) {
1298 transform_from_to(&c1, center->pro, &c2, pro);
1304 if (this_->ready == 3)
1309 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p)
1313 transform_set_center(this_->trans, c);
1314 transform_get_size(this_->trans, &width, &height);
1317 update_transformation(this_->trans, &po, p, NULL);
1321 navit_set_center_cursor(struct navit *this_, struct coord *cursor, int dir, int xpercent, int ypercent)
1326 transform_get_size(this_->trans, &width, &height);
1327 transform_set_yaw(this_->trans, dir);
1328 pn.x=xpercent*width/100;
1329 pn.y=ypercent*height/100;
1330 navit_set_center_coord_screen(this_, cursor, &pn);
1331 if (this_->ready == 3)
1337 navit_set_center_screen(struct navit *this_, struct point *p)
1341 transform_reverse(this_->trans, p, &c);
1344 pc.pro = transform_get_projection(this_->trans);
1345 navit_set_center(this_, &pc);
1349 navit_set_attr(struct navit *this_, struct attr *attr)
1351 int dir=0, orient_old=0, attr_updated=0;
1353 switch (attr->type) {
1355 if (this_->cursor_flag != !!attr->u.num) {
1356 this_->cursor_flag=!!attr->u.num;
1361 if(this_->layout_current!=attr->u.layout) {
1362 this_->layout_current=attr->u.layout;
1363 graphics_font_destroy_all(this_->gra);
1368 case attr_orientation:
1369 orient_old=this_->orientation;
1370 this_->orientation=attr->u.num;
1371 if (this_->orientation != -1) {
1372 dir = this_->orientation;
1374 if (this_->vehicle) {
1375 dir = this_->vehicle->dir;
1378 transform_set_yaw(this_->trans, dir);
1379 if (orient_old != this_->orientation) {
1380 if (this_->ready == 3)
1385 case attr_projection:
1386 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1387 navit_projection_set(this_, attr->u.projection);
1392 if (this_->tracking_flag != !!attr->u.num) {
1393 this_->tracking_flag=!!attr->u.num;
1398 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1402 if (((struct navit_vehicle *)l->data)->vehicle == attr->u.vehicle) {
1403 this_->vehicle=(struct navit_vehicle *)l->data;
1414 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1420 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1423 case attr_bookmark_map:
1424 attr->u.map=this_->bookmark;
1427 attr->u.num=this_->cursor_flag;
1429 case attr_destination:
1430 if (! this_->destination_valid)
1432 attr->u.pcoord=&this_->destination;
1434 case attr_former_destination_map:
1435 attr->u.map=this_->former_destination;
1440 iter->u.list=g_list_next(iter->u.list);
1442 iter->u.list=this_->layouts;
1446 attr->u.layout=(struct layout *)iter->u.list->data;
1448 attr->u.layout=this_->layout_current;
1452 if (iter && this_->mapsets) {
1453 if (!iter->u.mapset_handle) {
1454 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1456 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1458 mapset_close(iter->u.mapset_handle);
1465 case attr_navigation:
1466 attr->u.navigation=this_->navigation;
1468 case attr_orientation:
1469 attr->u.num=this_->orientation;
1471 case attr_projection:
1473 attr->u.num=transform_get_projection(this_->trans);
1479 attr->u.route=this_->route;
1482 attr->u.num=this_->tracking_flag;
1487 iter->u.list=g_list_next(iter->u.list);
1489 iter->u.list=this_->vehicles;
1493 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1495 if(this_->vehicle) {
1496 attr->u.vehicle=this_->vehicle->vehicle;
1503 attr->u.num=transform_get_scale(this_->trans);
1513 navit_add_log(struct navit *this_, struct log *log)
1515 struct attr type_attr;
1516 if (!log_get_attr(log, attr_type, &type_attr, NULL))
1518 if (!strcmp(type_attr.u.str, "textfile_debug")) {
1519 char *header = "type=track_tracked\n";
1520 if (this_->textfile_debug_log)
1522 log_set_header(log, header, strlen(header));
1523 this_->textfile_debug_log=log;
1530 navit_add_attr(struct navit *this_, struct attr *attr)
1533 switch (attr->type) {
1535 ret=navit_add_log(this_, attr->u.log);
1538 ret=navit_set_gui(this_, attr->u.gui);
1541 ret=navit_set_graphics(this_, attr->u.graphics);
1544 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1545 if(!this_->layout_current)
1546 this_->layout_current=attr->u.layout;
1550 this_->route=attr->u.route;
1553 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1555 case attr_navigation:
1556 this_->navigation=attr->u.navigation;
1558 case attr_recent_dest:
1559 this_->recentdest_count = attr->u.num;
1562 this_->speech=attr->u.speech;
1565 this_->tracking=attr->u.tracking;
1568 ret=navit_add_vehicle(this_, attr->u.vehicle);
1571 this_->center_timeout = attr->u.num;
1576 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1581 navit_attr_iter_new()
1583 return g_new0(struct attr_iter, 1);
1587 navit_attr_iter_destroy(struct attr_iter *iter)
1593 navit_add_callback(struct navit *this_, struct callback *cb)
1595 callback_list_add(this_->attr_cbl, cb);
1599 navit_remove_callback(struct navit *this_, struct callback *cb)
1601 callback_list_remove(this_->attr_cbl, cb);
1605 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
1607 * @param navit The navit instance
1612 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
1614 struct point cursor_pnt;
1615 enum projection pro;
1620 if (! vehicle_get_attr(nv->vehicle, attr_cursor, &cursor, NULL))
1622 if (! cursor.u.cursor)
1627 pro=transform_get_projection(this_->trans);
1628 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1630 cursor_draw(cursor.u.cursor, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans), nv->speed);
1635 pro=transform_get_projection(this_->trans);
1636 transform(this_->trans, pro, &nv->coord, &pnt2, 1, 0);
1639 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
1641 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
1647 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
1649 struct attr attr_dir, attr_speed, attr_pos;
1650 struct pcoord cursor_pc;
1651 struct point cursor_pnt, *pnt=&cursor_pnt;
1652 enum projection pro;
1654 int route_path_set=0;
1655 int recenter = 1; // indicates if we should recenter the map
1658 if (this_->ready != 3) {
1659 profile(0,"return 1\n");
1663 if (! vehicle_get_attr(nv->vehicle, attr_position_direction, &attr_dir, NULL) ||
1664 ! vehicle_get_attr(nv->vehicle, attr_position_speed, &attr_speed, NULL) ||
1665 ! vehicle_get_attr(nv->vehicle, attr_position_coord_geo, &attr_pos, NULL)) {
1666 profile(0,"return 2\n");
1669 nv->dir=*attr_dir.u.numd;
1670 nv->speed=*attr_speed.u.numd;
1671 pro=transform_get_projection(this_->trans);
1672 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
1673 if (nv != this_->vehicle) {
1674 navit_vehicle_draw(this_, nv, NULL);
1675 profile(0,"return 3\n");
1679 if (nv->speed < 10) {
1680 long long diff,diff_x,diff_y;
1682 diff_x = abs(nv->coord.x - nv->last.x);
1683 diff_y = abs(nv->coord.y - nv->last.y);
1684 diff = (diff_x * diff_x) + (diff_y * diff_y);
1686 if ((diff < 20) && (diff > 0)) { // if our long is only 32 bit wide, we could run into an overflow here
1691 nv->last = nv->coord;
1695 route_path_set=route_get_path_set(this_->route);
1696 cursor_pc.x = nv->coord.x;
1697 cursor_pc.y = nv->coord.y;
1698 cursor_pc.pro = pro;
1699 if (this_->tracking && this_->tracking_flag) {
1700 if (tracking_update(this_->tracking, &cursor_pc, nv->dir)) {
1701 nv->coord.x=cursor_pc.x;
1702 nv->coord.y=cursor_pc.y;
1705 if (nv->update_curr == 1) {
1707 if (this_->tracking && this_->tracking_flag)
1708 route_set_position_from_tracking(this_->route, this_->tracking);
1710 route_set_position(this_->route, &cursor_pc);
1712 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1714 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
1715 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
1716 if (!transform_within_border(this_->trans, &cursor_pnt, border)) {
1717 if (!this_->cursor_flag) {
1718 profile(0,"return 4\n");
1721 if ((nv->follow_curr != 1) && ((time(NULL) - this_->last_moved) > this_->center_timeout) && (this_->button_pressed != 1) && (recenter)) {
1722 if (this_->orientation != -1) {
1723 int dir=nv->dir-this_->orientation;
1724 navit_set_center_cursor(this_, &nv->coord, 0, 50 - 30.*sin(M_PI*dir/180.), 50 + 30.*cos(M_PI*dir/180.));
1727 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1733 if (this_->pid && nv->speed > 2)
1734 kill(this_->pid, SIGWINCH);
1736 if (this_->route && nv->update_curr == 1)
1737 navigation_update(this_->navigation, this_->route);
1738 if ((nv->follow_curr == 1) && (!this_->button_pressed)) {
1739 if (this_->cursor_flag && ((time(NULL) - this_->last_moved) > this_->center_timeout) && (recenter)) {
1740 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1742 } else { // We don't want to center, but redraw because otherwise the old route "lags"
1746 if (pnt && this_->route && !route_path_set && route_get_path_set(this_->route))
1748 if (nv->follow_curr > 1)
1751 nv->follow_curr=nv->follow;
1752 if (nv->update_curr > 1)
1755 nv->update_curr=nv->update;
1756 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
1758 navit_vehicle_draw(this_, nv, pnt);
1760 /* Finally, if we reached our destination, stop navigation. */
1761 if (this_->route && route_destination_reached(this_->route)) {
1762 navit_set_destination(this_, NULL, NULL);
1764 profile(0,"return 5\n");
1768 * Set the position of the vehicle
1770 * @param navit The navit instance
1771 * @param c The coordinate to set as position
1776 navit_set_position(struct navit *this_, struct pcoord *c)
1779 route_set_position(this_->route, c);
1780 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1781 if (this_->navigation) {
1782 navigation_update(this_->navigation, this_->route);
1784 map_dump_file(route_get_map(this_->route), "route.txt");
1785 map_dump_file(navigation_get_map(this_->navigation), "navigation.txt");
1789 if (this_->ready == 3)
1794 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
1800 * Register a new vehicle
1802 * @param navit The navit instance
1803 * @param v The vehicle instance
1804 * @returns 1 for success
1807 navit_add_vehicle(struct navit *this_, struct vehicle *v)
1809 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
1810 struct attr update,follow,color,active, color2, animate;
1816 nv->animate_cursor=0;
1817 if ((vehicle_get_attr(v, attr_update, &update, NULL)))
1818 nv->update=nv->update=update.u.num;
1819 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
1820 nv->follow=nv->follow=follow.u.num;
1821 if ((vehicle_get_attr(v, attr_color, &color, NULL)))
1822 nv->c=*(color.u.color);
1823 if ((vehicle_get_attr(v, attr_color2, &color2, NULL)))
1824 nv->c2=color2.u.color;
1827 nv->update_curr=nv->update;
1828 nv->follow_curr=nv->follow;
1829 this_->vehicles=g_list_append(this_->vehicles, nv);
1830 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
1831 navit_set_vehicle(this_, nv);
1832 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
1833 nv->animate_cursor=animate.u.num;
1834 nv->callback.type=attr_callback;
1835 nv->callback.u.callback=callback_new_2(callback_cast(navit_vehicle_update), this_, nv);
1836 vehicle_add_attr(nv->vehicle, &nv->callback);
1837 vehicle_set_attr(nv->vehicle, &this_->self, NULL);
1845 navit_get_gui(struct navit *this_)
1850 struct transformation *
1851 navit_get_trans(struct navit *this_)
1853 return this_->trans;
1857 navit_get_route(struct navit *this_)
1859 return this_->route;
1863 navit_get_navigation(struct navit *this_)
1865 return this_->navigation;
1868 struct displaylist *
1869 navit_get_displaylist(struct navit *this_)
1871 return this_->displaylist;
1875 navit_block(struct navit *this_, int block)
1878 this_->blocked |= 1;
1881 if (this_->blocked & 2) {
1891 navit_command_register(struct navit *this_, char *command, struct callback *cb)
1893 dbg(1,"registering '%s'\n", command);
1894 g_hash_table_insert(this_->commands, command, cb);
1900 navit_command_unregister(struct navit *this_, char *command)
1902 struct callback *ret=g_hash_table_lookup(this_->commands, command);
1904 g_hash_table_remove(this_->commands, command);
1911 navit_command_call_attrs(struct navit *this_, char *command, struct attr **in, struct attr ***out)
1913 struct callback *cb=g_hash_table_lookup(this_->commands, command);
1914 dbg(0,"calling callback %p for '%s'\n", cb, command);
1917 callback_call_args(cb, 3, command, in, out);
1922 navit_command_call(struct navit *this_, char *command)
1924 return navit_command_call_attrs(this_, command, NULL, NULL);
1928 navit_destroy(struct navit *this_)
1930 /* TODO: destroy objects contained in this_ */
1932 vehicle_destroy(this_->vehicle->vehicle);
1933 main_remove_navit(this_);
1934 char *center_file = navit_get_center_file(TRUE);
1935 navit_write_center_to_file(this_, center_file);
1936 g_free(center_file);
1937 callback_destroy(navit_command_unregister(this_, "zoom_in"));
1938 callback_destroy(navit_command_unregister(this_, "zoom_out"));
1939 callback_destroy(this_->nav_speech_cb);
1940 callback_destroy(this_->roadbook_callback);
1941 callback_destroy(this_->popup_callback);
1942 callback_destroy(this_->motion_timeout_callback);
1943 graphics_remove_callback(this_->gra, this_->resize_callback);
1944 callback_destroy(this_->resize_callback);
1945 graphics_remove_callback(this_->gra, this_->button_callback);
1946 callback_destroy(this_->button_callback);
1947 graphics_remove_callback(this_->gra, this_->motion_callback);
1948 callback_destroy(this_->motion_callback);
1949 g_hash_table_destroy(this_->commands);