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 navit_draw_displaylist(struct navit *this_)
200 if (this_->ready == 3)
201 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 1);
205 navit_handle_resize(struct navit *this_, int w, int h)
207 struct map_selection sel;
208 memset(&sel, 0, sizeof(sel));
213 transform_set_screen_selection(this_->trans, &sel);
215 graphics_resize(this_->gra, w, h);
216 if (this_->ready == 3)
221 navit_resize(void *data, int w, int h)
223 struct navit *this=data;
224 if (!this->ignore_graphics_events)
225 navit_handle_resize(this, w, h);
229 navit_get_width(struct navit *this_)
236 navit_get_height(struct navit *this_)
242 navit_popup(void *data)
244 struct navit *this_=data;
245 popup(this_, 1, &this_->pressed);
246 this_->button_timeout=NULL;
252 navit_ignore_button(struct navit *this_)
254 this_->ignore_button=1;
258 navit_ignore_graphics_events(struct navit *this_, int ignore)
260 this_->ignore_graphics_events=ignore;
264 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
268 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, (void *)pressed, (void *)button, p);
269 if (this_->ignore_button) {
270 this_->ignore_button=0;
278 this_->button_pressed=1;
282 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
285 navit_set_center_screen(this_, p);
287 popup(this_, button, p);
288 if (button == 4 && this_->use_mousewheel) {
290 navit_zoom_in(this_, 2, p);
292 if (button == 5 && this_->use_mousewheel) {
294 navit_zoom_out(this_, 2, p);
297 this_->button_pressed=0;
298 if (this_->button_timeout) {
299 event_remove_timeout(this_->button_timeout);
300 this_->button_timeout=NULL;
301 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
302 if (!this_->zoomed) {
303 this_->last_moved = time(NULL);
305 navit_set_center_screen(this_, p);
308 if (this_->motion_timeout) {
309 event_remove_timeout(this_->motion_timeout);
310 this_->motion_timeout=NULL;
315 transform_get_size(this_->trans, &pt.x, &pt.y);
318 pt.x-=this_->last.x-this_->pressed.x;
319 pt.y-=this_->last.y-this_->pressed.y;
320 graphics_draw_drag(this_->gra, NULL);
321 graphics_overlay_disable(this_->gra, 0);
322 if (!this_->zoomed) {
323 this_->last_moved = time(NULL);
325 navit_set_center_screen(this_, &pt);
333 navit_button(void *data, int pressed, int button, struct point *p)
335 struct navit *this=data;
336 if (!this->ignore_graphics_events) {
337 if (! this->popup_callback)
338 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
339 navit_handle_button(this, pressed, button, p, this->popup_callback);
345 navit_motion_timeout(struct navit *this_)
349 if (this_->drag_bitmap) {
351 point.x=(this_->current.x-this_->pressed.x);
352 point.y=(this_->current.y-this_->pressed.y);
353 if (graphics_draw_drag(this_->gra, &point)) {
354 graphics_overlay_disable(this_->gra, 1);
355 graphics_draw_mode(this_->gra, draw_mode_end);
357 this_->motion_timeout=NULL;
361 dx=(this_->current.x-this_->last.x);
362 dy=(this_->current.y-this_->last.y);
364 this_->last=this_->current;
365 graphics_overlay_disable(this_->gra, 1);
366 graphics_displaylist_move(this_->displaylist, dx, dy);
367 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 0);
370 this_->motion_timeout=NULL;
375 navit_handle_motion(struct navit *this_, struct point *p)
379 if (this_->button_pressed && !this_->popped) {
380 dx=(p->x-this_->pressed.x);
381 dy=(p->y-this_->pressed.y);
382 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
383 if (this_->button_timeout) {
384 event_remove_timeout(this_->button_timeout);
385 this_->button_timeout=NULL;
388 if (! this_->motion_timeout_callback)
389 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
390 if (! this_->motion_timeout)
391 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
397 navit_motion(void *data, struct point *p)
399 struct navit *this=data;
400 if (!this->ignore_graphics_events)
401 navit_handle_motion(this, p);
405 navit_scale(struct navit *this_, long scale, struct point *p)
407 struct coord c1, c2, *center;
409 transform_reverse(this_->trans, p, &c1);
410 transform_set_scale(this_->trans, scale);
412 transform_reverse(this_->trans, p, &c2);
413 center = transform_center(this_->trans);
414 center->x += c1.x - c2.x;
415 center->y += c1.y - c2.y;
421 * Change the current zoom level, zooming closer to the ground
423 * @param navit The navit instance
424 * @param factor The zoom factor, usually 2
425 * @param p The invariant point (if set to NULL, default to center)
429 navit_zoom_in(struct navit *this_, int factor, struct point *p)
431 long scale=transform_get_scale(this_->trans)/factor;
434 navit_scale(this_, scale, p);
438 * Change the current zoom level
440 * @param navit The navit instance
441 * @param factor The zoom factor, usually 2
442 * @param p The invariant point (if set to NULL, default to center)
446 navit_zoom_out(struct navit *this_, int factor, struct point *p)
448 long scale=transform_get_scale(this_->trans)*factor;
449 navit_scale(this_, scale, p);
453 navit_new(struct attr *parent, struct attr **attrs)
455 struct navit *this_=g_new0(struct navit, 1);
456 struct pcoord center;
459 enum projection pro=projection_mg;
465 main_add_navit(this_);
466 this_->self.type=attr_navit;
467 this_->self.u.navit=this_;
468 this_->attr_cbl=callback_list_new();
470 #if !defined(_WIN32) && !defined(__CEGCC__)
471 f=popen("pidof /usr/bin/ipaq-sleep","r");
473 fscanf(f,"%d",&this_->pid);
474 dbg(1,"ipaq_sleep pid=%d\n", this_->pid);
479 this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
481 this_->cursor_flag=1;
482 this_->orientation=-1;
483 this_->tracking_flag=1;
484 this_->recentdest_count=10;
486 this_->last_moved = 0;
487 this_->center_timeout = 10;
488 this_->use_mousewheel = 1;
490 for (;*attrs; attrs++) {
491 switch((*attrs)->type) {
493 zoom=(*attrs)->u.num;
496 g=*((*attrs)->u.coord_geo);
499 this_->cursor_flag=!!(*attrs)->u.num;
501 case attr_orientation:
502 this_->orientation=(*attrs)->u.num;
505 this_->tracking_flag=!!(*attrs)->u.num;
507 case attr_recent_dest:
508 this_->recentdest_count=(*attrs)->u.num;
510 case attr_drag_bitmap:
511 this_->drag_bitmap=!!(*attrs)->u.num;
513 case attr_use_mousewheel:
514 this_->use_mousewheel=!!(*attrs)->u.num;
517 this_->center_timeout = (*attrs)->u.num;
520 dbg(0, "Unexpected attribute %x\n",(*attrs)->type);
524 transform_from_geo(pro, &g, &co);
529 this_->trans=transform_new();
530 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
531 this_->displaylist=graphics_displaylist_new();
532 this_->commands=g_hash_table_new(g_str_hash, g_str_equal);
533 navit_command_register(this_, "zoom_in", callback_new_3(callback_cast(navit_zoom_in), this_, (void *)2, NULL));
534 navit_command_register(this_, "zoom_out", callback_new_3(callback_cast(navit_zoom_out), this_, (void *)2, NULL));
539 navit_set_gui(struct navit *this_, struct gui *gui)
544 if (gui_has_main_loop(this_->gui)) {
545 if (! main_loop_gui) {
546 main_loop_gui=this_->gui;
548 dbg(0,"gui with main loop already active, ignoring this instance");
556 navit_set_graphics(struct navit *this_, struct graphics *gra)
561 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
562 graphics_add_callback(gra, this_->resize_callback);
563 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
564 graphics_add_callback(gra, this_->button_callback);
565 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
566 graphics_add_callback(gra, this_->motion_callback);
571 navit_get_graphics(struct navit *this_)
577 navit_projection_set(struct navit *this_, enum projection pro)
582 c=transform_center(this_->trans);
583 transform_to_geo(transform_get_projection(this_->trans), c, &g);
584 transform_set_projection(this_->trans, pro);
585 transform_from_geo(pro, &g, c);
590 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
593 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
608 while ((ch = fgetc(f)) != EOF) {
609 if ((ch == '\n') && (prev != '\n')) {
615 if (prev != '\n') { // Last line did not end with a newline
620 f = fopen(file, "r+");
622 while (lines >= limit) { // We have to "scroll up"
624 numc = 0; // Counts how many bytes we have in our line to scroll up
625 while ((ch = fgetc(f)) != EOF) {
632 buffer=g_malloc(numc);
633 offset = numc; // Offset holds where we currently are
636 fseek(f,offset,SEEK_SET);
637 readc = fread(buffer,1,numc,f);
639 fseek(f,-(numc+readc),SEEK_CUR);
640 fwrite(buffer,1,readc,f);
643 } while (readc == numc);
647 ftruncate(fd,(offset-numc));
661 prostr = projection_to_name(c->pro);
662 fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
663 prostr, *prostr ? ":" : "",
664 c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
665 c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y,
674 * navit_get_user_data_directory
676 * returns the directory used to store user data files (center.txt,
677 * destination.txt, bookmark.txt, ...)
679 * arg: gboolean create: create the directory if it does not exist
682 navit_get_user_data_directory(gboolean create) {
684 dir = getenv("NAVIT_USER_DATADIR");
685 if (create && !file_exists(dir)) {
686 dbg(0,"creating dir %s\n", dir);
687 if (file_mkdir(dir,0)) {
688 dbg(0,"failed creating dir %s\n", dir);
697 * navit_get_destination_file
699 * returns the name of the file used to store destinations with its
702 * arg: gboolean create: create the directory where the file is stored
703 * if it does not exist
706 navit_get_destination_file(gboolean create)
708 return g_strjoin(NULL, navit_get_user_data_directory(create), "/destination.txt", NULL);
712 * navit_get_bookmark_file
714 * returns the name of the file used to store bookmarks with its
717 * arg: gboolean create: create the directory where the file is stored
718 * if it does not exist
721 navit_get_bookmark_file(gboolean create)
723 return g_strjoin(NULL, navit_get_user_data_directory(create), "/bookmark.txt", NULL);
728 * navit_get_bookmark_file
730 * returns the name of the file used to store the center file with its
733 * arg: gboolean create: create the directory where the file is stored
734 * if it does not exist
737 navit_get_center_file(gboolean create)
739 return g_strjoin(NULL, navit_get_user_data_directory(create), "/center.txt", NULL);
743 navit_set_center_from_file(struct navit *this_, char *file)
748 size_t line_size = 0;
750 struct coord *center;
752 f = fopen(file, "r");
755 getline(&line, &line_size, f);
758 center = transform_center(this_->trans);
759 pro = transform_get_projection(this_->trans);
760 coord_parse(g_strchomp(line), pro, center);
767 navit_write_center_to_file(struct navit *this_, char *file)
771 struct coord *center;
773 f = fopen(file, "w+");
775 center = transform_center(this_->trans);
776 pro = transform_get_projection(this_->trans);
777 coord_print(pro, center, f);
787 * Start the route computing to a given set of coordinates
789 * @param navit The navit instance
790 * @param c The coordinate to start routing to
791 * @param description A label which allows the user to later identify this destination in the former destinations selection
795 navit_set_destination(struct navit *this_, struct pcoord *c, char *description)
798 this_->destination=*c;
799 this_->destination_valid=1;
801 this_->destination_valid=0;
802 char *destination_file = navit_get_destination_file(TRUE);
803 navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
804 g_free(destination_file);
805 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
807 route_set_destination(this_->route, c);
808 if (this_->navigation) {
809 navigation_flush(this_->navigation);
810 navigation_update(this_->navigation, this_->route);
813 if (this_->ready == 3)
819 * @brief Checks if a route is calculated
821 * This function checks if a route is calculated.
823 * @param this_ The navit struct whose route should be checked.
824 * @return True if the route is set, false otherwise.
827 navit_check_route(struct navit *this_)
830 return route_get_path_set(this_->route);
837 * Record the given set of coordinates as a bookmark
839 * @param navit The navit instance
840 * @param c The coordinate to store
841 * @param description A label which allows the user to later identify this bookmark
845 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
847 char *bookmark_file = navit_get_bookmark_file(TRUE);
848 navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
849 g_free(bookmark_file);
851 callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
854 struct navit *global_navit;
857 navit_add_bookmarks_from_file(struct navit *this_)
859 char *bookmark_file = navit_get_bookmark_file(FALSE);
860 struct attr parent={attr_navit, .u.navit=this_};
861 struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
862 struct attr *attrs[]={&type, &data, NULL};
864 this_->bookmark=map_new(&parent, attrs);
865 g_free(bookmark_file);
869 navit_former_destinations_active(struct navit *this_)
871 char *destination_file = navit_get_destination_file(FALSE);
875 f=fopen(destination_file,"r");
877 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
881 g_free(destination_file);
886 navit_add_former_destinations_from_file(struct navit *this_)
888 char *destination_file = navit_get_destination_file(FALSE);
889 struct attr parent={attr_navit, .u.navit=this_};
890 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
891 struct attr *attrs[]={&type, &data, NULL};
898 this_->former_destination=map_new(&parent, attrs);
899 g_free(destination_file);
900 if (!this_->route || !navit_former_destinations_active(this_))
902 mr=map_rect_new(this_->former_destination, NULL);
903 while ((item=map_rect_get_item(mr))) {
904 if (item->type == type_former_destination && item_coord_get(item, &c, 1))
907 map_rect_destroy(mr);
908 pc.pro=map_projection(this_->former_destination);
912 route_set_destination(this_->route, &pc);
913 this_->destination=pc;
914 this_->destination_valid=1;
920 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
925 if (this_->textfile_debug_log && this_->vehicle) {
926 str1=g_strdup_vprintf(fmt, ap);
927 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
928 log_write(this_->textfile_debug_log, str2, strlen(str2));
936 navit_say(struct navit *this_, char *text)
938 speech_say(this_->speech, text);
942 navit_speak(struct navit *this_)
944 struct navigation *nav=this_->navigation;
945 struct map *map=NULL;
946 struct map_rect *mr=NULL;
951 map=navigation_get_map(nav);
953 mr=map_rect_new(map, NULL);
955 while ((item=map_rect_get_item(mr)) && item->type == type_nav_position);
956 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
957 speech_say(this_->speech, attr.u.str);
958 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
960 map_rect_destroy(mr);
965 navit_window_roadbook_update(struct navit *this_)
967 struct navigation *nav=this_->navigation;
968 struct map *map=NULL;
969 struct map_rect *mr=NULL;
972 struct param_list param[5];
976 datawindow_mode(this_->roadbook_window, 1);
978 map=navigation_get_map(nav);
980 mr=map_rect_new(map, NULL);
981 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
983 dbg(0,"while loop\n");
984 while ((item=map_rect_get_item(mr))) {
985 dbg(0,"item=%p\n", item);
987 if (item->type != type_nav_position) {
988 item_attr_get(item, attr_navigation_long, &attr);
989 dbg(2, "Command='%s'\n", attr.u.str);
990 param[0].value=g_strdup(attr.u.str);
992 param[0].value=_("Position");
993 param[0].name=_("Command");
995 item_attr_get(item, attr_length, &attr);
996 dbg(2, "Length=%d\n", attr.u.num);
997 param[1].name=_("Length");
999 if ( attr.u.num >= 2000 )
1001 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1005 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1008 item_attr_get(item, attr_time, &attr);
1009 dbg(2, "Time=%d\n", attr.u.num);
1011 param[2].name=_("Time");
1014 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1018 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1021 item_attr_get(item, attr_destination_length, &attr);
1022 dbg(2, "Destlength=%d\n", attr.u.num);
1023 param[3].name=_("Destination Length");
1024 if ( attr.u.num >= 2000 )
1026 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1030 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1033 item_attr_get(item, attr_destination_time, &attr);
1034 dbg(2, "Desttime=%d\n", attr.u.num);
1036 param[4].name=_("Destination Time");
1039 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1043 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1045 datawindow_add(this_->roadbook_window, param, 5);
1047 map_rect_destroy(mr);
1049 datawindow_mode(this_->roadbook_window, 0);
1053 navit_window_roadbook_destroy(struct navit *this_)
1056 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1057 this_->roadbook_window=NULL;
1058 this_->roadbook_callback=NULL;
1061 navit_window_roadbook_new(struct navit *this_)
1063 if (this_->roadbook_callback || this_->roadbook_window) {
1067 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1068 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1069 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1070 navit_window_roadbook_update(this_);
1074 get_direction(char *buffer, int angle, int mode)
1079 sprintf(buffer,"%d",angle);
1082 if (angle < 69 || angle > 291)
1084 if (angle > 111 && angle < 249)
1086 if (angle > 22 && angle < 158)
1088 if (angle > 202 && angle < 338)
1093 angle=(angle+15)/30;
1096 sprintf(buffer,"%d H", angle);
1101 struct navit_window_items {
1102 struct datawindow *win;
1103 struct callback *click;
1111 navit_window_items_click(struct navit *this_, struct navit_window_items *nwi, char **col)
1117 dbg(0,"enter col=%s,%s,%s,%s,%s\n", col[0], col[1], col[2], col[3], col[4]);
1118 sscanf(col[4], "0x%x,0x%x", &c.x, &c.y);
1119 c.pro = projection_mg;
1120 dbg(0,"0x%x,0x%x\n", c.x, c.y);
1121 description=g_strdup_printf("%s %s", nwi->name, col[3]);
1122 navit_set_destination(this_, &c, description);
1123 g_free(description);
1127 navit_window_items_open(struct navit *this_, struct navit_window_items *nwi)
1129 struct map_selection sel;
1130 struct coord c,*center;
1131 struct mapset_handle *h;
1133 struct map_rect *mr;
1137 struct param_list param[5];
1142 dbg(0, "distance=%d\n", nwi->distance);
1143 if (nwi->distance == -1)
1146 dist=nwi->distance*1000;
1147 param[0].name="Distance";
1148 param[1].name="Direction";
1149 param[2].name="Type";
1150 param[3].name="Name";
1154 sel.order[layer_town]=18;
1155 sel.order[layer_street]=18;
1156 sel.order[layer_poly]=18;
1159 sel.range=item_range_all;
1161 center=transform_center(this_->trans);
1162 sel.u.c_rect.lu.x=center->x-dist;
1163 sel.u.c_rect.lu.y=center->y+dist;
1164 sel.u.c_rect.rl.x=center->x+dist;
1165 sel.u.c_rect.rl.y=center->y-dist;
1166 dbg(2,"0x%x,0x%x - 0x%x,0x%x\n", sel.u.c_rect.lu.x, sel.u.c_rect.lu.y, sel.u.c_rect.rl.x, sel.u.c_rect.rl.y);
1167 nwi->click=callback_new_2(callback_cast(navit_window_items_click), this_, nwi);
1168 nwi->win=gui_datawindow_new(this_->gui, nwi->name, nwi->click, NULL);
1169 h=mapset_open(navit_get_mapset(this_));
1170 while ((m=mapset_next(h, 1))) {
1172 dbg(2,"m=%p %s\n", m, map_get_filename(m));
1174 mr=map_rect_new(m, &sel);
1175 dbg(2,"mr=%p\n", mr);
1176 while ((item=map_rect_get_item(mr))) {
1177 if (item_coord_get(item, &c, 1)) {
1178 if (coord_rect_contains(&sel.u.c_rect, &c) && g_hash_table_lookup(nwi->hash, &item->type)) {
1179 if (! item_attr_get(item, attr_label, &attr))
1181 idist=transform_distance(map_projection(item->map), center, &c);
1183 get_direction(dirbuf, transform_get_angle_delta(center, &c, 0), 1);
1184 param[0].value=distbuf;
1185 param[1].value=dirbuf;
1186 param[2].value=item_to_name(item->type);
1187 sprintf(distbuf,"%d", idist/1000);
1188 param[3].value=attr.u.str;
1189 sprintf(coordbuf, "0x%x,0x%x", c.x, c.y);
1190 param[4].value=coordbuf;
1191 datawindow_add(nwi->win, param, 5);
1193 /* printf("gefunden %s %s %d\n",item_to_name(item->type), attr.u.str, idist/1000); */
1195 if (item->type >= type_line)
1196 while (item_coord_get(item, &c, 1));
1199 map_rect_destroy(mr);
1204 struct navit_window_items *
1205 navit_window_items_new(const char *name, int distance)
1207 struct navit_window_items *nwi=g_new0(struct navit_window_items, 1);
1208 nwi->name=g_strdup(name);
1209 nwi->distance=distance;
1210 nwi->hash=g_hash_table_new(g_int_hash, g_int_equal);
1216 navit_window_items_add_item(struct navit_window_items *nwi, enum item_type type)
1218 nwi->list=g_list_prepend(nwi->list, (void *)type);
1219 g_hash_table_insert(nwi->hash, &nwi->list->data, (void *)1);
1223 navit_add_window_items(struct navit *this_, struct navit_window_items *nwi)
1225 this_->windows_items=g_list_append(this_->windows_items, nwi);
1229 navit_add_menu_windows_items(struct navit *this_, struct menu *men)
1231 struct navit_window_items *nwi;
1232 struct callback *cb;
1234 l=this_->windows_items;
1237 cb=callback_new_2(callback_cast(navit_window_items_open), this_, nwi);
1238 menu_add(men, nwi->name, menu_type_menu, cb);
1244 navit_init(struct navit *this_)
1249 struct navit_vehicle *nv;
1253 navit_destroy(this_);
1257 dbg(0,"no graphics\n");
1258 navit_destroy(this_);
1261 if (gui_set_graphics(this_->gui, this_->gra)) {
1262 struct attr attr_type_gui, attr_type_graphics;
1263 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1264 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1265 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1266 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1267 dbg(0," for explanations and solutions\n");
1269 navit_destroy(this_);
1272 graphics_init(this_->gra);
1275 dbg(1,"parsed one vehicle\n");
1277 nv->callback.type=attr_callback;
1278 nv->callback.u.callback=callback_new_2(callback_cast(navit_vehicle_update), this_, nv);
1279 vehicle_add_attr(nv->vehicle, &nv->callback);
1280 vehicle_set_attr(nv->vehicle, &this_->self, NULL);
1283 if (this_->mapsets) {
1284 ms=this_->mapsets->data;
1286 if ((map=route_get_map(this_->route)))
1287 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1288 if ((map=route_get_graph_map(this_->route))) {
1289 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1290 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1292 route_set_mapset(this_->route, ms);
1293 route_set_projection(this_->route, transform_get_projection(this_->trans));
1295 if (this_->tracking) {
1296 tracking_set_mapset(this_->tracking, ms);
1298 tracking_set_route(this_->tracking, this_->route);
1300 if (this_->navigation) {
1301 if ((map=navigation_get_map(this_->navigation))) {
1302 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1303 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1306 if (this_->tracking) {
1307 if ((map=tracking_get_map(this_->tracking))) {
1308 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1309 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1312 navit_add_bookmarks_from_file(this_);
1313 navit_add_former_destinations_from_file(this_);
1315 if (this_->navigation && this_->speech) {
1316 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1317 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1319 char *center_file = navit_get_center_file(FALSE);
1320 navit_set_center_from_file(this_, center_file);
1321 g_free(center_file);
1323 if (this_->menubar) {
1324 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1326 navit_add_menu_windows_items(this_, men);
1332 navit_window_roadbook_new(this_);
1333 navit_window_items_new(this_);
1335 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1337 if (this_->ready == 3)
1342 navit_zoom_to_route(struct navit *this_)
1345 struct map_rect *mr;
1348 struct coord_rect r;
1349 int count=0,scale=16;
1352 map=route_get_map(this_->route);
1355 mr=map_rect_new(map, NULL);
1358 while ((item=map_rect_get_item(mr))) {
1359 while (item_coord_get(item, &c, 1)) {
1363 coord_rect_extend(&r, &c);
1369 c.x=(r.rl.x+r.lu.x)/2;
1370 c.y=(r.rl.y+r.lu.y)/2;
1371 dbg(0,"count=%d\n",count);
1372 ct=transform_center(this_->trans);
1374 dbg(0,"%x,%x-%x,%x\n", r.rl.x,r.rl.y,r.lu.x,r.lu.y);
1375 while (scale < 1<<20) {
1377 transform_set_scale(this_->trans, scale);
1378 transform_setup_source_rect(this_->trans);
1379 transform(this_->trans, transform_get_projection(this_->trans), &r.lu, &p1, 1, 0);
1380 transform(this_->trans, transform_get_projection(this_->trans), &r.rl, &p2, 1, 0);
1381 dbg(0,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1382 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1383 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1389 if (this_->ready == 3)
1394 * Change the current zoom level
1396 * @param navit The navit instance
1397 * @param center The point where to center the map, including its projection
1401 navit_set_center(struct navit *this_, struct pcoord *center)
1403 struct coord *c=transform_center(this_->trans);
1405 enum projection pro = transform_get_projection(this_->trans);
1406 if (pro != center->pro) {
1409 transform_from_to(&c1, center->pro, &c2, pro);
1415 if (this_->ready == 3)
1420 navit_set_center_cursor(struct navit *this_, struct coord *cursor, int dir, int xpercent, int ypercent)
1422 struct coord *c=transform_center(this_->trans);
1427 transform_get_size(this_->trans, &width, &height);
1429 transform_set_angle(this_->trans, dir);
1430 p.x=(100-xpercent)*width/100;
1431 p.y=(100-ypercent)*height/100;
1432 transform_reverse(this_->trans, &p, &cnew);
1434 if (this_->ready == 3)
1440 navit_set_center_screen(struct navit *this_, struct point *p)
1444 transform_reverse(this_->trans, p, &c);
1447 pc.pro = transform_get_projection(this_->trans);
1448 navit_set_center(this_, &pc);
1452 navit_set_attr(struct navit *this_, struct attr *attr)
1454 int dir=0, orient_old=0, attr_updated=0;
1456 switch (attr->type) {
1458 if (this_->cursor_flag != !!attr->u.num) {
1459 this_->cursor_flag=!!attr->u.num;
1464 if(this_->layout_current!=attr->u.layout) {
1465 this_->layout_current=attr->u.layout;
1466 graphics_font_destroy_all(this_->gra);
1471 case attr_orientation:
1472 orient_old=this_->orientation;
1473 this_->orientation=attr->u.num;
1474 if (this_->orientation != -1) {
1475 dir = this_->orientation;
1477 if (this_->vehicle) {
1478 dir = this_->vehicle->dir;
1481 transform_set_angle(this_->trans, dir);
1482 if (orient_old != this_->orientation) {
1483 if (this_->ready == 3)
1488 case attr_projection:
1489 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1490 navit_projection_set(this_, attr->u.projection);
1495 if (this_->tracking_flag != !!attr->u.num) {
1496 this_->tracking_flag=!!attr->u.num;
1501 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1505 if (((struct navit_vehicle *)l->data)->vehicle == attr->u.vehicle) {
1506 this_->vehicle=(struct navit_vehicle *)l->data;
1517 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1523 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1526 case attr_bookmark_map:
1527 attr->u.map=this_->bookmark;
1530 attr->u.num=this_->cursor_flag;
1532 case attr_destination:
1533 if (! this_->destination_valid)
1535 attr->u.pcoord=&this_->destination;
1537 case attr_former_destination_map:
1538 attr->u.map=this_->former_destination;
1543 iter->u.list=g_list_next(iter->u.list);
1545 iter->u.list=this_->layouts;
1549 attr->u.layout=(struct layout *)iter->u.list->data;
1551 attr->u.layout=this_->layout_current;
1555 if (iter && this_->mapsets) {
1556 if (!iter->u.mapset_handle) {
1557 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1559 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1561 mapset_close(iter->u.mapset_handle);
1568 case attr_navigation:
1569 attr->u.navigation=this_->navigation;
1571 case attr_orientation:
1572 attr->u.num=this_->orientation;
1574 case attr_projection:
1576 attr->u.num=transform_get_projection(this_->trans);
1582 attr->u.route=this_->route;
1585 attr->u.num=this_->tracking_flag;
1590 iter->u.list=g_list_next(iter->u.list);
1592 iter->u.list=this_->vehicles;
1596 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1598 if(this_->vehicle) {
1599 attr->u.vehicle=this_->vehicle->vehicle;
1606 attr->u.num=transform_get_scale(this_->trans);
1616 navit_add_log(struct navit *this_, struct log *log)
1618 struct attr type_attr;
1619 if (!log_get_attr(log, attr_type, &type_attr, NULL))
1621 if (!strcmp(type_attr.u.str, "textfile_debug")) {
1622 char *header = "type=track_tracked\n";
1623 if (this_->textfile_debug_log)
1625 log_set_header(log, header, strlen(header));
1626 this_->textfile_debug_log=log;
1633 navit_add_attr(struct navit *this_, struct attr *attr)
1635 switch (attr->type) {
1637 return navit_add_log(this_, attr->u.log);
1639 return navit_set_gui(this_, attr->u.gui);
1641 return navit_set_graphics(this_, attr->u.graphics);
1643 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1644 if(!this_->layout_current)
1645 this_->layout_current=attr->u.layout;
1648 this_->route=attr->u.route;
1651 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1653 case attr_navigation:
1654 this_->navigation=attr->u.navigation;
1656 case attr_recent_dest:
1657 this_->recentdest_count = attr->u.num;
1660 this_->speech=attr->u.speech;
1663 this_->tracking=attr->u.tracking;
1666 return navit_add_vehicle(this_, attr->u.vehicle);
1669 this_->center_timeout = attr->u.num;
1678 navit_attr_iter_new()
1680 return g_new0(struct attr_iter, 1);
1684 navit_attr_iter_destroy(struct attr_iter *iter)
1690 navit_add_callback(struct navit *this_, struct callback *cb)
1692 callback_list_add(this_->attr_cbl, cb);
1696 navit_remove_callback(struct navit *this_, struct callback *cb)
1698 callback_list_remove(this_->attr_cbl, cb);
1702 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
1704 * @param navit The navit instance
1709 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
1711 struct point cursor_pnt;
1712 enum projection pro;
1717 if (! vehicle_get_attr(nv->vehicle, attr_cursor, &cursor, NULL))
1719 if (! cursor.u.cursor)
1724 pro=transform_get_projection(this_->trans);
1725 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0);
1727 cursor_draw(cursor.u.cursor, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_angle(this_->trans, 0), nv->speed);
1732 pro=transform_get_projection(this_->trans);
1733 transform(this_->trans, pro, &nv->coord, &pnt2, 1, 0);
1736 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
1738 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
1744 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
1746 struct attr attr_dir, attr_speed, attr_pos;
1747 struct pcoord cursor_pc;
1748 struct point cursor_pnt, *pnt=&cursor_pnt;
1749 enum projection pro;
1751 int route_path_set=0;
1752 int recenter = 1; // indicates if we should recenter the map
1755 if (this_->ready != 3) {
1756 profile(0,"return 1\n");
1760 if (! vehicle_get_attr(nv->vehicle, attr_position_direction, &attr_dir, NULL) ||
1761 ! vehicle_get_attr(nv->vehicle, attr_position_speed, &attr_speed, NULL) ||
1762 ! vehicle_get_attr(nv->vehicle, attr_position_coord_geo, &attr_pos, NULL)) {
1763 profile(0,"return 2\n");
1766 nv->dir=*attr_dir.u.numd;
1767 nv->speed=*attr_speed.u.numd;
1768 pro=transform_get_projection(this_->trans);
1769 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
1770 if (nv != this_->vehicle) {
1771 navit_vehicle_draw(this_, nv, NULL);
1772 profile(0,"return 3\n");
1776 if (nv->speed < 10) {
1777 long long diff,diff_x,diff_y;
1779 diff_x = abs(nv->coord.x - nv->last.x);
1780 diff_y = abs(nv->coord.y - nv->last.y);
1781 diff = (diff_x * diff_x) + (diff_y * diff_y);
1783 if ((diff < 20) && (diff > 0)) { // if our long is only 32 bit wide, we could run into an overflow here
1788 nv->last = nv->coord;
1792 route_path_set=route_get_path_set(this_->route);
1793 cursor_pc.x = nv->coord.x;
1794 cursor_pc.y = nv->coord.y;
1795 cursor_pc.pro = pro;
1796 if (this_->tracking && this_->tracking_flag) {
1797 if (tracking_update(this_->tracking, &cursor_pc, nv->dir)) {
1798 nv->coord.x=cursor_pc.x;
1799 nv->coord.y=cursor_pc.y;
1802 if (nv->update_curr == 1) {
1804 if (this_->tracking && this_->tracking_flag)
1805 route_set_position_from_tracking(this_->route, this_->tracking);
1807 route_set_position(this_->route, &cursor_pc);
1809 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1811 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
1812 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0);
1813 if (!transform_within_border(this_->trans, &cursor_pnt, border)) {
1814 if (!this_->cursor_flag) {
1815 profile(0,"return 4\n");
1818 if ((nv->follow_curr != 1) && ((time(NULL) - this_->last_moved) > this_->center_timeout) && (this_->button_pressed != 1) && (recenter)) {
1819 if (this_->orientation != -1) {
1820 int dir=nv->dir-this_->orientation;
1821 navit_set_center_cursor(this_, &nv->coord, 0, 50 - 30.*sin(M_PI*dir/180.), 50 + 30.*cos(M_PI*dir/180.));
1824 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1830 if (this_->pid && nv->speed > 2)
1831 kill(this_->pid, SIGWINCH);
1833 if (this_->route && nv->update_curr == 1)
1834 navigation_update(this_->navigation, this_->route);
1835 if ((nv->follow_curr == 1) && (!this_->button_pressed)) {
1836 if (this_->cursor_flag && ((time(NULL) - this_->last_moved) > this_->center_timeout) && (recenter)) {
1837 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1839 } else { // We don't want to center, but redraw because otherwise the old route "lags"
1843 if (pnt && this_->route && !route_path_set && route_get_path_set(this_->route))
1845 if (nv->follow_curr > 1)
1848 nv->follow_curr=nv->follow;
1849 if (nv->update_curr > 1)
1852 nv->update_curr=nv->update;
1853 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
1855 navit_vehicle_draw(this_, nv, pnt);
1857 /* Finally, if we reached our destination, stop navigation. */
1858 if (this_->route && route_destination_reached(this_->route)) {
1859 navit_set_destination(this_, NULL, NULL);
1861 profile(0,"return 5\n");
1865 * Set the position of the vehicle
1867 * @param navit The navit instance
1868 * @param c The coordinate to set as position
1873 navit_set_position(struct navit *this_, struct pcoord *c)
1876 route_set_position(this_->route, c);
1877 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1878 if (this_->navigation) {
1879 navigation_update(this_->navigation, this_->route);
1881 map_dump_file(route_get_map(this_->route), "route.txt");
1882 map_dump_file(navigation_get_map(this_->navigation), "navigation.txt");
1886 if (this_->ready == 3)
1891 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
1897 * Register a new vehicle
1899 * @param navit The navit instance
1900 * @param v The vehicle instance
1901 * @returns 1 for success
1904 navit_add_vehicle(struct navit *this_, struct vehicle *v)
1906 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
1907 struct attr update,follow,color,active, color2, animate;
1913 nv->animate_cursor=0;
1914 if ((vehicle_get_attr(v, attr_update, &update, NULL)))
1915 nv->update=nv->update=update.u.num;
1916 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
1917 nv->follow=nv->follow=follow.u.num;
1918 if ((vehicle_get_attr(v, attr_color, &color, NULL)))
1919 nv->c=*(color.u.color);
1920 if ((vehicle_get_attr(v, attr_color2, &color2, NULL)))
1921 nv->c2=color2.u.color;
1924 nv->update_curr=nv->update;
1925 nv->follow_curr=nv->follow;
1926 this_->vehicles=g_list_append(this_->vehicles, nv);
1927 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
1928 navit_set_vehicle(this_, nv);
1929 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
1930 nv->animate_cursor=animate.u.num;
1938 navit_get_gui(struct navit *this_)
1943 struct transformation *
1944 navit_get_trans(struct navit *this_)
1946 return this_->trans;
1950 navit_get_route(struct navit *this_)
1952 return this_->route;
1956 navit_get_navigation(struct navit *this_)
1958 return this_->navigation;
1961 struct displaylist *
1962 navit_get_displaylist(struct navit *this_)
1964 return this_->displaylist;
1968 navit_block(struct navit *this_, int block)
1971 this_->blocked |= 1;
1974 if (this_->blocked & 2) {
1984 navit_command_register(struct navit *this_, char *command, struct callback *cb)
1986 dbg(1,"registering '%s'\n", command);
1987 g_hash_table_insert(this_->commands, command, cb);
1993 navit_command_unregister(struct navit *this_, char *command)
1995 struct callback *ret=g_hash_table_lookup(this_->commands, command);
1997 g_hash_table_remove(this_->commands, command);
2004 navit_command_call(struct navit *this_, char *command)
2006 struct callback *cb=g_hash_table_lookup(this_->commands, command);
2007 dbg(0,"calling callback %p for '%s'\n", cb, command);
2010 callback_call_1(cb, command);
2015 navit_destroy(struct navit *this_)
2017 /* TODO: destroy objects contained in this_ */
2018 main_remove_navit(this_);
2019 char *center_file = navit_get_center_file(TRUE);
2020 navit_write_center_to_file(this_, center_file);
2021 g_free(center_file);
2022 callback_destroy(navit_command_unregister(this_, "zoom_in"));
2023 callback_destroy(navit_command_unregister(this_, "zoom_out"));
2024 g_hash_table_destroy(this_->commands);